-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination-sum-ii.cpp
More file actions
28 lines (24 loc) · 906 Bytes
/
combination-sum-ii.cpp
File metadata and controls
28 lines (24 loc) · 906 Bytes
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
// https://leetcode.com/problems/combination-sum-ii
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
std::vector<std::vector<int>> res;
std::vector<int> subres;
std::sort(candidates.begin(), candidates.end());
auto dfs = [&](auto&& self, int target, int start) -> void {
if (target == 0) {
res.push_back(subres);
return;
}
for (uint64_t i = start; i < candidates.size(); ++i) {
if (target < candidates[i]) return;
if (i > start && candidates[i] == candidates[i - 1]) continue;
subres.push_back(candidates[i]);
self(self, target - candidates[i], i + 1);
subres.pop_back();
}
};
dfs(dfs, target, 0);
return res;
}
};