40. Combination Sum II (Recursion)

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

class Solution {
public:
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        results.clear();
        vector<int> result;
        sort(candidates.begin(), candidates.end());
        dfs(candidates, result,target, 0);
        return results;
    }

    void dfs(vector<int> &candidates, vector<int> &result, int target, int depth)
    {
        if(candidates[depth] > target || depth == candidates.size()) return;
        result.push_back(candidates[depth]);
        if(candidates[depth] == target)
        {
            results.push_back(result);
            result.pop_back();
            return;
        }
        dfs(candidates,result, target-candidates[depth], depth+1);
        result.pop_back();
        while(++depth<candidates.size()  && candidates[depth] == candidates[depth-1]); //avoid repetition
        dfs(candidates,result, target, depth);
    }
private:
    vector<vector<int> > results;
};
时间: 2024-08-24 14:28:49

40. Combination Sum II (Recursion)的相关文章

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

[Leetcode][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),find all unique combinations in C where the candi

leetCode 40.Combination Sum II(组合总和II) 解题思路和方法

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode OJ 40. Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

【一天一道LeetCode】#40. Combination Sum II

一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including

LeetCode:40. Combination Sum II(Medium)

1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合 注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次:(2)数组存在重复元素:(3)数组中都是正整数:(4)不能存在重复解 3. 解题思路 这与第39题 Combination Sum 看起来很是类似,但一些细节要求完全不

LeetCode 40. Combination Sum II (组合的和之二)

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

leetcode 40 Combination Sum II --- java ----100%

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

Java [Leetcode 40]Combination Sum II

题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will b