-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcombinationSum.cpp
More file actions
30 lines (26 loc) · 840 Bytes
/
combinationSum.cpp
File metadata and controls
30 lines (26 loc) · 840 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
class Solution {
public:
void findCombination(int ind, int target, vector<int>&candidates, vector<vector<int>>&ans, vector<int>&ds)
{
// base condition
if(ind==candidates.size()){
if(target==0){
ans.push_back(ds);
}
return;
}
//take
if(candidates[ind]<=target){
ds.push_back(candidates[ind]);
findCombination(ind, target-candidates[ind], candidates, ans, ds);
ds.pop_back();
}
findCombination(ind+1, target, candidates, ans, ds);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>>ans;
vector<int>ds;
findCombination(0, target, candidates, ans, ds);
return ans;
}
};