-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path39.cpp
More file actions
29 lines (27 loc) · 900 Bytes
/
Copy path39.cpp
File metadata and controls
29 lines (27 loc) · 900 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
class Solution {
public:
vector<vector<int>> combinationSum(vector<int> &candidates, int target) {
for (int i = 0; i < candidates.size(); ++i) {
vector<int> temp;
temp.push_back(candidates[i]);
int index = 0;
helper(candidates, target, candidates[i], temp, i);
}
return ret;
}
void helper(vector<int> &candidates, int &target, int sum, vector<int> &temp, int index) {
if (sum == target)
ret.push_back(temp);
if (sum >= target)
return;
for (int i = index; i < candidates.size(); ++i) {
if (sum + candidates[i] > target)
continue;
temp.push_back(candidates[i]);
helper(candidates, target, sum + candidates[i], temp, i);
temp.erase(temp.end() - 1);
}
}
private:
vector<vector<int>> ret;
};