-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum_II.cpp
59 lines (54 loc) · 1.97 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int binarySearch(vector<int> &candidates, int target, int left) {
int right = candidates.size()-1;
int mid = 0;
while(left<=right) {
mid = (left+right)/2;
if(target==candidates[mid]) {
return mid;
}else if(target<candidates[mid]) {
right = mid-1;
}else{
left = mid+1;
}
}
return -1;
}
void helper(vector<int> &candidates, int target, vector<vector<int>>&result, vector<int> &row, int small_idx) {
if(target<candidates[0]) return;
int idx = binarySearch(candidates, target, small_idx);
if(idx!=-1) {
row.push_back(candidates[idx]);
if(result.empty()) {
result.push_back(row);
}else{
bool unique = false; bool skip = false;
for(int k=0; k<result.size(); k++) {
vector<int> v1 = result[k];
unique = false;
for(int i=0; i<v1.size(); i++) {
if(v1[i]!=row[i]) {
unique = true; break;
}
}
if(!unique) { skip = true; break;}
}
if(!skip) result.push_back(row);
}
row.pop_back();
}
for(int i=small_idx; i<candidates.size(); i++) {
int new_target = target-candidates[i];
row.push_back(candidates[i]);
helper(candidates, new_target, result, row, i+1);
row.pop_back();
}
}
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
vector<vector<int>> result;
if(candidates.size()<1) return result;
sort(candidates.begin(), candidates.end());
vector<int> row;
int small_idx=0;
helper(candidates, target, result, row, small_idx);
return result;
}