-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination-sum.cpp
30 lines (27 loc) · 971 Bytes
/
combination-sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
void combinationSum(vector<int> &candidates, int target, vector<int> &combination, vector<vector<int>> &result, int idx) {
if (target < 0) return;
else if (target == 0) {
result.push_back(combination);
return;
}
for (int i=idx; i<candidates.size(); i++) {
combination.push_back(candidates[i]);
combinationSum(candidates, target - candidates[i], combination, result, i);
combination.pop_back();
}
}
public:
/**
* @param candidates: A list of integers
* @param target:An integer
* @return: A list of lists of integers
*/
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> result;
vector<int> combination;
combinationSum(candidates, target, combination, result, 0);
return result;
}
};