-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40-Combination-Sum-II.cpp
32 lines (31 loc) · 1.01 KB
/
40-Combination-Sum-II.cpp
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
class Solution {
public:
vector<int> res;
vector<vector<int>> ans;
set<vector<int>> hashset;
void combination(vector<int>& candidates, const vector<int>::iterator ¤t_iter, int target){
if(target == 0){
vector<int> temp(res.begin(), res.end());
if(hashset.find(temp) == hashset.end()){
hashset.insert(temp);
ans.push_back(temp);
}
return ;
}
for(vector<int>::iterator iter = current_iter; iter != candidates.end(); iter++){
if(target - *iter >= 0){
res.push_back(*iter);
combination(candidates, iter + 1, target - *iter);
res.pop_back();
}
else{
return ;
}
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
combination(candidates, candidates.begin(), target);
return ans;
}
};