-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
33 lines (33 loc) · 1.11 KB
/
Solution.java
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
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
return f(candidates,target,candidates.length-1);
}
public List<List<Integer>> f(int[] candidates, int target,int l) {
List<List<Integer>> ans=new ArrayList<List<Integer>>();
if (candidates[0]>target||target<0||l<0) {
return ans;
}
for (int i =l; i >=0 ; i--) {
int k=candidates[i];
if (k==target) {
List<Integer> list=new ArrayList<Integer>();
list.add(k);
ans.add(list);
}else {
List<List<Integer>> q=f(candidates,target-k,i-1);
if (q!=null&&q.size()!=0) {
for (int j = 0; j < q.size(); j++) {
List<Integer> list=q.get(j);
list.add(k);
ans.add(list);
}
}
}
while (i>0&&candidates[i]==candidates[i-1]) {
i--;
}
}
return ans;
}
}