# Combination Sum

class Solution {
private:
vector<int> c;
vector<vector<int> > res;  //保存最后结果
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<int> a;
sort(candidates.begin(), candidates.end()); //先对C中候选数升序排序，为后面的剪枝做准备
c = candidates;
backtrack(a, 0, target);
//for(vector<int> &m:res)
//sort(m.begin(), m.end());
//res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
int sum(vector<int>& a){
int res = 0;
for (int i = 0; i<a.size(); i++){
res += a[i];
}
return res;
};
void backtrack(vector<int>& a, int k, int target)
{
if (k == c.size()) return;
if (sum(a) == target){
res.push_back(a);
return;
}
else if (sum(a)>target)
return;
else {
//k = k + 1;
for (int i = k; i<c.size(); i++) {
//a[k] = c[i];
a.push_back(c[i]);
backtrack(a, i, target);
a.pop_back();

}
}
}
};

#### 回溯详解及其应用：Leetcode 39 combination sum

2017-01-31 16:47:37

#### leetcode 39. Combination Sum-回溯算法|递归|非递归

2016-03-15 17:37:26

#### 回溯法——combination-sum、combination-sum-ii

2016-05-31 13:27:11

#### 377. Combination Sum IV-动态规划

2016-09-22 19:54:56

#### leetcode笔记：Combination Sum

2015-11-25 23:59:24

#### Leetcode #39. Combination Sum 组合求和 解题报告

2016-04-10 23:42:47

#### 【LeetCode】Combination Sum I & II 解题报告

2014-12-16 11:06:12

#### [LeetCode39]Combination Sum

2014-06-17 04:15:23

#### [leetcode]40. Combination Sum II ,python实现【medium难度】

2016-06-16 17:37:04

#### Leetcode 39 Combination Sum & 40 Combination Sum II & 216 Combination Sum III & 377 Combination V

2016-07-26 13:38:06