forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (30 loc) · 885 Bytes
/
Solution.java
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
class Solution {
private List<List<Integer>> ans;
private int[] candidates;
private int target;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
ans = new ArrayList<>();
Arrays.sort(candidates);
this.target = target;
this.candidates = candidates;
dfs(0, 0, new ArrayList<>());
return ans;
}
private void dfs(int u, int s, List<Integer> t) {
if (s > target) {
return;
}
if (s == target) {
ans.add(new ArrayList<>(t));
return;
}
for (int i = u; i < candidates.length; ++i) {
if (i > u && candidates[i] == candidates[i - 1]) {
continue;
}
t.add(candidates[i]);
dfs(i + 1, s + candidates[i], t);
t.remove(t.size() - 1);
}
}
}