-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00040-combination_sum_II.cpp
58 lines (45 loc) · 1.41 KB
/
00040-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
// 40: Combination Sum Ii
// https://leetcode.com/problems/combination-sum-ii
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
private:
void backtrack(vector<int>& candidates, int target, int sum, int start, vector<int>& current, vector<vector<int>>& result) {
if (sum > target) return;
if (sum == target) {
result.push_back(current);
return;
}
for (int i=start; i<candidates.size(); i++) {
if (i>start && candidates[i] == candidates[i-1]) continue;
current.push_back(candidates[i]);
backtrack(candidates, target, sum + candidates[i], i+1, current, result);
current.pop_back();
}
}
public:
// SOLUTOIN
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
std::sort(candidates.begin(), candidates.end());
vector<int> current;
vector<vector<int>> result;
backtrack(candidates, target, 0, 0, current, result);
return result;
}
};
int main() {
Solution o;
// INPUT
vector<int> candidates = {2,5,2,1,2};
int target = 5;
// OUTPUT
auto result = o.combinationSum2(candidates, target);
cout << "["; for (auto x : result) {
cout << "["; for (auto y : x) {
cout << y << ",";
} cout << "\b],";
} cout << "\b]" << endl;
return 0;
}