-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135.h
More file actions
33 lines (29 loc) · 989 Bytes
/
135.h
File metadata and controls
33 lines (29 loc) · 989 Bytes
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
31
32
33
class Solution {
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) {
// write your code here
vector<vector<int>> res;
vector<int> r;
sort(candidates.begin(), candidates.end());
combinationSum(candidates, target, 0, r, res);
return res;
}
void combinationSum(const vector<int> &candidates, int target, int start,
vector<int> &r, vector<vector<int>> &res){
if(target == 0){
res.push_back(r);
return;
}
for(int i = start; i < candidates.size(); i++){
if(candidates[i] > target) break;
r.push_back(candidates[i]);
combinationSum(candidates, target - candidates[i], i, r, res);
r.pop_back();
}
}
};