-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCombination Sum II.cpp
More file actions
61 lines (59 loc) · 1.47 KB
/
Combination Sum II.cpp
File metadata and controls
61 lines (59 loc) · 1.47 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
55
56
57
58
59
60
61
void MakeAns(int curr,int target,vector<int>& path, vector<vector<int> >& ans,vector<int>& A)
{
if(curr==A.size())
{
if(target==0)
ans.push_back(path);
return;
}
if(target<0)
return;
int val = A[curr], end = curr;
while(end<A.size() && A[end]==val)
end++;
int count = end - curr;
for(int i=0;i<=count;i++)
{
MakeAns(end,target,path,ans,A);
path.push_back(val);
target-=val;
}
path.erase(path.begin()+path.size()-count-1,path.end());
return;
}
vector<vector<int> > Solution::combinationSum(vector<int> &A, int target) {
sort(A.begin(),A.end());
vector<vector<int> > ans;
vector<int> path;
MakeAns(0,target,path,ans,A);
return ans;
}
/*void util(vector<vector<int>> &res, vector<int> v, vector<int> &A, int T, int sum, int n){
if(sum>T || n<0)
return;
if(sum==T){
sort(v.begin(),v.end());
if(find(res.begin(),res.end(),v)==res.end())
res.push_back(v);
return;
}
if(sum+A[n-1]<=T){
sum+=A[n-1];
v.push_back(A[n-1]);
util(res,v,A,T,sum,n-1);
v.pop_back();
sum-=A[n-1];
util(res,v,A,T,sum,n-1);
}
else
util(res,v,A,T,sum,n-1);
return;
}
vector<vector<int>> Solution::combinationSum(vector<int> &A, int B){
vector<vector<int>>res;
vector<int> v;
util(res,v,A,B,0,A.size());
sort(res.begin(),res.end());
return res;
}
*/