-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_39.cpp
More file actions
25 lines (25 loc) · 816 Bytes
/
Copy pathleetcode_39.cpp
File metadata and controls
25 lines (25 loc) · 816 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
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> combination;
backtrack(candidates, target, 0, combination, result);
return result;
}
private:
void backtrack(vector<int>& candidates, int remain, int start,
vector<int>& combination, vector<vector<int>>& result){
if(remain == 0) {
result.push_back(combination);
return;
}
if(remain < 0) {
return;
}
for(int i = start; i < candidates.size(); i++){
combination.push_back(candidates[i]);
backtrack(candidates, remain - candidates[i], i, combination, result);
combination.pop_back();
}
}
};