-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum2.java
More file actions
26 lines (25 loc) · 853 Bytes
/
CombinationSum2.java
File metadata and controls
26 lines (25 loc) · 853 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
public class CombinationSum2 {
ArrayList<ArrayList<Integer>> ans;
public ArrayList<ArrayList<Integer>> combinationSum2(int[] can, int x) {
ans = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tmp = new ArrayList<Integer>();
Arrays.sort(can);
solve(can,x, 0, tmp);
return ans;
}
void solve(int[] can, int x, int from, ArrayList<Integer> tmp){
if(x == 0){
ans.add(new ArrayList<Integer>(tmp));
}else{
int last = -1;
for(int i = from; i < can.length; i++){
if(x >= can[i] && can[i] != last){
last = can[i];
tmp.add(can[i]);
solve(can, x - can[i], i+1, tmp);
tmp.remove(tmp.size() -1);
}
}
}
}
}