[LeetCode] 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 positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ 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<int> num;
    int target;
    vector<vector<int> > result;
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        this->num = num;
        this->target = target;

        if(num.size()==0){
            vector<int> tmp;
            result.push_back(tmp);
            return result;
        }

        sort(this->num.begin(),this->num.end());
        for(int i=0;i<num.size();i++){
           vector<int> tmp(1,this->num[i]);
           recursion(i+1,tmp,this->num[i]);

        }

        return result;
    }
private:
    void recursion(int start,vector<int> tmp,int sum){
        if(sum == target && find(result.begin(),result.end(),tmp)==result.end()){
            result.push_back(tmp);
            return;
        }
        vector<int> tmp0(tmp);
        int sum0 = sum;
        for(int i=start;i<num.size();i++){
            tmp.push_back(num[i]);
            sum += num[i];
            if(sum<target)
               recursion(i+1,tmp,sum);
            else if(sum == target){
                if(find(result.begin(),result.end(),tmp)==result.end())
                    result.push_back(tmp);
            }
            else
                break;
            tmp = tmp0;
            sum = sum0;
        }
    }//end func
};
时间: 2024-10-25 12:49:59

[LeetCode] Combination Sum II (递归)的相关文章

LeetCode: Combination Sum II [039]

[题目] 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

LeetCode: Combination Sum 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 ta

[LeetCode] 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——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] 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 Combination Sum II python

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 Combination Sum II (DFS)

题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是1+2=3,而不是两个. DFS解决,每次考虑candidates[i]取还是不取,但是这样子还是会产生重复,这里只需要一个技巧就可以使得没有重复出现.如果当前元素已经被考虑过取了,那么在考虑不取的时候,i后面的与candidates[i]相同的都要跳过.观察一下可以发现,如果有一串相同的数字出现的

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi

[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