-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path40.cpp
35 lines (29 loc) · 936 Bytes
/
40.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> temp;
sort(candidates.begin(), candidates.end());
dfs(0, target, candidates, temp, res);
return res;
}
void dfs(int idx, int _target, vector<int>& candidates, vector<int>& temp, vector<vector<int>>& res) {
if (_target == 0) {
res.emplace_back(temp);
return;
}
int n = candidates.size();
int pre = -1; // 增加条件
for (int i = idx; i < n; ++i) {
int val = candidates[i];
if (val <= _target && val != pre) {
temp.push_back(val);
dfs(i+1, _target-val, candidates, temp, res);
temp.pop_back();
}
pre = val;
}
}
};