-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40.combination-sum-ii.java
More file actions
31 lines (23 loc) · 896 Bytes
/
Copy path40.combination-sum-ii.java
File metadata and controls
31 lines (23 loc) · 896 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
29
30
31
class Solution {
List<List<Integer>> res;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
solve(candidates, target, 0, new ArrayList<Integer>());
return res;
}
public void solve(int[] candidates, int target, int i, List<Integer> curr){
if(target == 0){
res.add(new ArrayList<Integer>(curr));
return;
}
if(i>= candidates.length || candidates[i] > target)
return;
curr.add(candidates[i]);
solve(candidates, target-candidates[i], i+1, curr);
curr.remove(curr.size()-1);
while(i+1<candidates.length && candidates[i] == candidates[i+1])
i++;
solve(candidates, target, i+1, curr);
}
}