-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40_combinationSum2.cpp
More file actions
30 lines (30 loc) · 856 Bytes
/
40_combinationSum2.cpp
File metadata and controls
30 lines (30 loc) · 856 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
class Solution {
public:
vector<int> row;
vector<vector<int>> res;
void dfs(vector<int>& candidates, int target,int index){
if(target == 0){
res.push_back(row);
return;
}
int last = -1;
for(int i = index;i<candidates.size();i++){
if(candidates[i] == last)
continue;
else
last = candidates[i];
if(candidates[i]<=target){
row.push_back(candidates[i]);
dfs(candidates,target-candidates[i],i+1);
row.pop_back();
}else{
return;
}
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(begin(candidates),end(candidates));
dfs(candidates,target,0);
return res;
}
};