-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCombination Sum II (bazinga)
More file actions
30 lines (30 loc) · 975 Bytes
/
Combination Sum II (bazinga)
File metadata and controls
30 lines (30 loc) · 975 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
public class Solution {
static List<List<Integer>> list=new ArrayList<List<Integer>>();
public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
list.clear();
Arrays.sort(candidates);
List<Integer> tmp=new ArrayList<Integer>();
dfs(0,target,candidates,tmp);
HashSet<List<Integer>> hashSet=new HashSet<List<Integer>>();
for (int i = 0; i < list.size(); i++) {
hashSet.add(list.get(i));
}
list.clear();
list.addAll(hashSet);
return list;
}
private static void dfs(int start,int target,int []candidates,List<Integer> tmp){
if(target==0){
List<Integer> a=new ArrayList<Integer>();
a.addAll(tmp);
list.add(a);
return;
}
for (int i = start; i < candidates.length; i++) {
if(candidates[i]>target) return;
tmp.add(candidates[i]);
dfs(i+1, target-candidates[i], candidates, tmp);
tmp.remove(tmp.size()-1);
}
}
}