-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcombination_sum_ii.cpp
More file actions
52 lines (43 loc) · 1.24 KB
/
combination_sum_ii.cpp
File metadata and controls
52 lines (43 loc) · 1.24 KB
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
class Solution {
public:
// 参考Combination Sum,这里更简单,同一个数不需要重复使用。
void combination_sum_2(vector<int> &num,
int target,
int start,
int sum,
vector<int> &combination,
vector<vector<int> > &results) {
int size = num.size();
if (sum == target) {
results.push_back(combination);
}
else if ((start < size) && (sum < target)) {
for (int i = start; i < size; ) {
int val = num[i];
combination.push_back(val);
// 变化在这步
combination_sum_2(num, target, i + 1, sum + val, combination, results);
combination.pop_back();
// 这里还是需要考虑去重,考虑num = [1, 1, 2],target = 2的情况。
int j = i + 1;
while (j < size) {
if (val == num[j]) {
++j;
}
else {
break;
}
}
i = j;
}
}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int> > results;
vector<int> combination;
// 这里排序是为了保证输出顺序
sort(num.begin(), num.end());
combination_sum_2(num, target, 0, 0, combination, results);
return results;
}
};