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 Solution {
List<List<Integer>> result = new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if (target <= 0 || candidates == null) return result;
Arrays.sort(candidates);
ArrayList<Integer> temp = new ArrayList<Integer>();
recur(candidates, 0, new ArrayList<Integer>(), target);
return result;
}
private void recur(int []candidates, int index, List<Integer> fromPrev, int target){
if (target == 0){
List<Integer> temp = new ArrayList<Integer>(fromPrev);
result.add(temp);
return;
}
if (index == candidates.length || target < 0) return;
for (int i = index; i < candidates.length; i++){
if (!(i > index && candidates[i] == candidates[i-1])){
fromPrev.add(candidates[i]);
recur(candidates, i+1, fromPrev, target-candidates[i]);
fromPrev.remove(fromPrev.size()-1);
}
}
}
}
|
No comments:
Post a Comment