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