-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination_Sum_II.cpp
46 lines (39 loc) · 1.06 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
class Solution {
public:
static bool myCmp(const int &a, const int &b) {
return a < b;
}
bool isDup(vector<vector<int> > &v, vector<int> &tmp) {
for(int i = 0; i < v.size(); ++i) {
if(v[i] == tmp)
return true;
}
return false;
}
void combinationSum2Recursion(int index, int cur_sum, int target, vector<int> tmp,
vector<vector<int> > &v, vector<int> &num) {
if(cur_sum == target) {
if(!isDup(v, tmp)) {
v.push_back(tmp);
}
return;
}
if(index >= num.size())
return;
combinationSum2Recursion(index+1, cur_sum, target, tmp, v, num);
if(cur_sum + num[index] <= target) {
tmp.push_back(num[index]);
combinationSum2Recursion(index+1, cur_sum+num[index], target, tmp, v, num);
}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int> > v;
int size = num.size();
if(size == 0)
return v;
sort(num.begin(), num.end(), myCmp);
vector<int> tmp;
combinationSum2Recursion(0, 0, target, tmp, v, num);
return v;
}
};