-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_sum_39.cpp
More file actions
54 lines (46 loc) · 1.5 KB
/
Copy pathcombination_sum_39.cpp
File metadata and controls
54 lines (46 loc) · 1.5 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
void backtrack(vector<int>& nums,int target,int ind,vector<int> &res,vector<vector<int>> &ans)
{
if(target==0){
ans.push_back(res);
return;
}
if(ind>=nums.size())
return;
if(target<0)
return;
backtrack(nums,target,ind+1,res,ans);
res.push_back(nums[ind]);
backtrack(nums,target-nums[ind],ind,res,ans);
res.pop_back();
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> res;
backtrack(candidates,target,0,res,ans);
return ans;
}
};
// class Solution {
// public:
// void findCombination(int idx, int target, vector<int> candidates, vector<vector<int>> &ans, vector<int> &ds){
// if(idx == candidates.size()){
// if(target == 0) ans.push_back(ds);
// return;
// }
// if(candidates[idx] <= target){
// ds.push_back(candidates[idx]);
// findCombination(idx, target-candidates[idx], candidates, ans, ds);
// ds.pop_back();
// }
// findCombination(idx+1, target, candidates, ans, ds);
// return;
// }
// vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
// vector<vector<int>> ans;
// vector<int> ds;
// findCombination(0, target, candidates, ans, ds);
// return ans;
// }
// };