forked from namigoel/Hacktober-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination-Sum
41 lines (33 loc) · 995 Bytes
/
Combination-Sum
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
class Solution {
public:
void helper(vector<int> c,int t,vector<vector<int>> &vec,vector<int> &v,int s){
if(s>t) {
return ;
}
if(s==t){
cout<<" "<<s;
vector<int> vv;
vv=v;
sort(vv.begin(),vv.end());
vector<vector<int>>::iterator it;
it = find (vec.begin(), vec.end(), vv);
if (it == vec.end())
vec.push_back(vv);
return ;
}
for(int i=0;i<c.size();i++){
v.push_back(c[i]);
helper(c,t,vec,v,s+c[i]);
// if(v.size()>0)
v.pop_back();
}
// if(v.size()>0)
// v.pop_back();
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> vec;
vector<int> v;
helper(candidates,target,vec,v,0);
return vec;
}
};