forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0040.cpp
More file actions
26 lines (24 loc) · 811 Bytes
/
0040.cpp
File metadata and controls
26 lines (24 loc) · 811 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
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> path;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, path, ans);
return ans;
}
private:
void dfs(vector<int>& candidates, int target, int s, vector<int>& path, vector<vector<int>>& ans) {
if (target < 0) return;
if (target == 0) {
ans.push_back(path);
return;
}
for (int i = s; i < candidates.size(); i++) {
if (i > s && candidates[i] == candidates[i - 1]) continue;
path.push_back(candidates[i]);
dfs(candidates, target - candidates[i], i + 1, path, ans);
path.pop_back();
}
}
};