-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCombination Sum II (by Raymond)
More file actions
44 lines (44 loc) · 1.35 KB
/
Combination Sum II (by Raymond)
File metadata and controls
44 lines (44 loc) · 1.35 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
34
35
36
37
38
39
40
41
42
43
44
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(candidates.length>0&&target==candidates[0]){
res.add(new ArrayList<Integer>());
res.get(0).add(target);
return res;
}
if(candidates.length==0||target<candidates[0]){
return res;
}
int[] arr = new int[candidates.length-1];
int i = 0;
while(i<arr.length){
arr[i]=candidates[i+1];
i++;
}
List<List<Integer>> res1 = combinationSum2(arr,target-candidates[0]);
List<List<Integer>> res2 = combinationSum2(arr,target);
i = 0;
while(i<res1.size()){
res1.get(i).add(0,candidates[0]);
res.add(res1.get(i));
i++;
}
i=0;
while(i<res2.size()){
res.add(res2.get(i));
i++;
}
List<List<Integer>> list = new ArrayList<List<Integer>>();
Set<List<Integer>> set = new HashSet<List<Integer>>();
i = 0;
while(i<res.size()){
if(!set.contains(res.get(i))){
list.add(res.get(i));
set.add(res.get(i));
}
i++;
}
return list;
}
}