forked from Viv786ek/Problem-Of-The-Day
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.cpp
More file actions
30 lines (24 loc) · 787 Bytes
/
Combination Sum.cpp
File metadata and controls
30 lines (24 loc) · 787 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
void calSum(vector<int>A,int B,vector<vector<int>>&res,vector<int>&temp,int index){
if(B==0){
res.push_back(temp);
return;
}
for(int i=index;i<A.size();++i){
if(B-A[i] >=0){
temp.push_back(A[i]);
B -= A[i];
calSum(A,B,res,temp,i);
B += A[i];
temp.pop_back();
}
}
}
vector<vector<int> > combinationSum(vector<int> &A, int B) {
// Your code here
sort(A.begin(),A.end());
A.erase(unique(A.begin(),A.end()),A.end());
vector<int>temp;
vector<vector<int>> res;
calSum(A,B,res,temp,0);
return res;
}