-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumII.java
More file actions
33 lines (29 loc) · 1.05 KB
/
Copy pathCombinationSumII.java
File metadata and controls
33 lines (29 loc) · 1.05 KB
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombinationSumII {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
combination(candidates, target, 0, new ArrayList<Integer>(), res);
return res;
}
private void combination(int[] candidates, int target, int start, List<Integer> one, List<List<Integer>> res){
int len = candidates.length;
if(target == 0){
res.add(new ArrayList<Integer>(one));
}else{
for(int i = start; i < len; i++){
if(i > start && candidates[i] == candidates[i - 1]) continue;
if(candidates[i] > target) return;
one.add(candidates[i]);
combination(candidates, target - candidates[i], i + 1, one, res);
one.remove(one.size() - 1);
}
}
}
public static void main(String[] args) {
CombinationSumII combination = new CombinationSumII();
System.out.println(combination.combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8));
}
}