-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.cpp
More file actions
executable file
·32 lines (30 loc) · 1.03 KB
/
combinationSum.cpp
File metadata and controls
executable file
·32 lines (30 loc) · 1.03 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
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> out;
sort(candidates.begin(), candidates.end());
for (int i = 0; i< candidates.size() && candidates[i]<=target;i++)
{
int tp = candidates[i];
int sub = target - tp;
if(sub == 0)
{
vector<int> tv;
tv.push_back(tp);
out.push_back(tv);
return out;
}else if(sub > 0)
{
vector<int> newCan(candidates.begin()+i,candidates.end());
vector<vector<int>> rp = combinationSum(newCan, sub);
for(int j = 0; j<rp.size();j++){
rp[j].insert(rp[j].begin(),tp);
out.push_back(rp[j]);
}
}
}
return out;
}
};