[Leetcode] Combination Sum / Combination Sum II

Combination Sum Mar 7 '12 5736 / 16047

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

The same repeated number may be chosen from C unlimited number of times.

Note:

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

 

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

class Solution {
public:
    vector<vector<int> > res;
    int n;
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<int> r;
        res.clear();
        n = candidates.size();
        sort(candidates.begin(), candidates.end());
        gen(r,0,0, candidates, target);
        return res;
    }
    
    void gen(vector<int> &r,int sum,int l, const vector<int>& can, int tar) {
        if (sum == tar) {
            res.push_back(r);
            return;
        }
        if (sum > tar) return ;
        
        for (int i = l; i < n; i++) {
            r.push_back(can[i]);
            sum += can[i];
            gen(r, sum,i, can, tar);
            sum -= can[i];
            r.pop_back();
        }
        
    }
};

@2013-10-05

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<vector<int> > res;
        if (candidates.size() == 0) return res;
        vector<int> v;
        sort(candidates.begin(), candidates.end());
        gen(candidates, res, v, target, 0);
        return res;
    }
    
    void gen(vector<int>& can, vector<vector<int> >& res, vector<int>& v, int left, int cur) {
        if (left == 0) {
            res.push_back(v);
            return;
        }
        if (left < 0 || cur >= can.size()) return;
        
        int next = cur + 1;
        while (next < can.size() && can[next] == can[cur]) next++;
        
        int t = left, cnt = 0;
        gen(can, res, v, t, next);
        while (t > 0) {
            v.push_back(can[cur]);
            t -= can[cur];
            cnt ++;
            gen(can, res, v, t, next);
        }
        while (cnt-- > 0) v.pop_back();
    }
};

 Combination Sum II

Mar 7 '12 4451 / 13371

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 (a1a2, � , 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] 

» Solve this problem
class Solution {
public:
    vector<vector<int> > res;
    int n;
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        vector<int> r;
        res.clear();
        n = candidates.size();
        sort(candidates.begin(), candidates.end());
        gen(r,0,0, candidates, target);
        return res;
    }
    
    void gen(vector<int> &r,int sum,int l, const vector<int>& can, int tar) {
        if (sum == tar) {
            res.push_back(r);
            return;
        }
        if (sum > tar) return ;
        
        for (int i = l; i < n; i++) {
            if (i > l && can[i] == can[i-1]) {continue;}
            r.push_back(can[i]);
            sum += can[i];
            gen(r, sum, i+1, can, tar);
            sum -= can[i];
            r.pop_back();
        }
        
    }
};

@2013-10-05

class Solution {
public:
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        vector<vector<int> > res;
        if (num.size() == 0) return res;
        vector<int> v;
        sort(num.begin(), num.end());
        gen(num, target, res, v, 0);
        return res;
    }
    
    void gen(const vector<int> &a, int tar, vector<vector<int> >& res, vector<int>& v, int cur) {
        if (tar == 0) {
            res.push_back(v);
            return;
        }
        if (tar < 0 || cur >= a.size()) return;
        
        int next = cur + 1;
        while (next < a.size() && a[next] == a[cur]) next++;
        gen(a,tar, res, v, next);
        if (a[cur] <= tar) {
            v.push_back(a[cur]);
            gen(a, tar - a[cur], res, v, cur + 1);
            v.pop_back();
        }
    }
};

猜你喜欢

转载自cozilla.iteye.com/blog/1927438