leetcode 39: Combination Sum

原创 2015年07月08日 15:34:41
class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int> > res;
        vector<int> set;
        sort(candidates.begin(),candidates.end());
        helper(candidates,0,target,set,res);
        return res;
    }
    void helper(vector<int>& candidates,int idx,int target,vector<int>& set,vector<vector<int> >& res){
        for(int i=idx;i<candidates.size()&&candidates[i]<=target;i++)
        {
            set.push_back(candidates[i]);
            if(candidates[i]==target)
            {
                res.push_back(set);
                set.pop_back();
                break;
            }
            else
            {
                helper(candidates,i,target-candidates[i],set,res);
                set.pop_back();
            }
        }
    }
};

相关文章推荐

LeetCode 39. Combination Sum II

和LeetCode 38. Combination Sum

leetcode【39+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合,使得组合中数字的和是target值。并且组合里的数字可以重复,也就是不限制C中每一个数字的使用。 这明显是回溯啦,大家记得让...

LeetCode39 Combination Sum

原题:英:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uni...

【一天一道LeetCode】#39. Combination Sum

一天一道LeetCode系列(一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique ...

39. Combination Sum [leetcode][javascript解法]

Combination Sum Add to List QuestionEditorial Solution My Submissions Total Accepted: 128797 To...
  • dayuqi
  • dayuqi
  • 2016年12月13日 23:00
  • 129

LeetCode OJ-39-Combination Sum

题目:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w...

[leetcode] 39& 40 Combination Sum I& II

if (i != index && num[i] == num[i - 1]) continue; Given a set of candidate numbers (C) and a target...

leetcode - 39. Combination Sum

39. Combination Sum Given a set ofcandidate numbers (C) (without duplicates) and atarget number (T)...
  • laeen
  • laeen
  • 2017年02月27日 11:10
  • 185

leetcode 39. Combination Sum

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

【LeetCode】39. Combination Sum

【LeetCode】39. Combination Sum
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 39: Combination Sum
举报原因:
原因补充:

(最多只允许输入30个字)