-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (28 loc) · 930 Bytes
/
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
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> ans = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTracking(candidates, target, 0);
return ans;
}
private void backTracking(int[] candidates, int target, int startIndex){
if(sum == target){
ans.add(new ArrayList(path));
}
for(int i = startIndex; i < candidates.length; i++){
if(sum + candidates[i] > target){
break;
}
if(i > startIndex && candidates[i] == candidates[i-1]){
continue;
}
sum += candidates[i];
path.add(candidates[i]);
backTracking(candidates, target, i+1);
sum -= candidates[i];
path.removeLast();
}
}
}