-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path39.cpp
More file actions
22 lines (22 loc) · 704 Bytes
/
39.cpp
File metadata and controls
22 lines (22 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<vector<int>> res;
void backtracking(vector<int>& candidates, int start, int remain, vector<int>& path) {
if (remain == 0) {
res.push_back(path);
return;
}
int n = candidates.size();
for (int i = start; i < n; i++) {
if (remain < candidates[i]) continue;
path.push_back(candidates[i]);
backtracking(candidates, i, remain - candidates[i], path);
path.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> path;
backtracking(candidates, 0, target, path);
return res;
}
};