# 39. Combination Sum

Given a set of candidate numbers (C) (without duplicates) 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.
• 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]
]


## code

class Solution {
void backtracking(vector<vector<int>> & res,vector<int> & temp,vector<int> & candidates,int remain,int start){
if(remain<0)      return;
if(remain==0){
res.push_back(temp);
return;
}
for(int i=start;i<candidates.size();++i){
temp.push_back(candidates[i]);
backtracking(res,temp,candidates,remain-candidates[i],i);//notice here
temp.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int> & candidates, int target) {
vector<int> temp;
vector<vector<int>> res;
backtracking(res,temp,candidates,target,0);
return res;
}
};


### 学籍管理系统文档

#### 北邮教务系统评教脚本

Published on September 17, 2017

#### 72.Edit distance

Published on September 17, 2017