-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumII.java
More file actions
28 lines (27 loc) · 957 Bytes
/
CombinationSumII.java
File metadata and controls
28 lines (27 loc) · 957 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
// 40. Combination Sum II
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
backtrack(0, candidates, new ArrayList<>(), result, target);
return result;
}
private void backtrack(int index, int[] nums,List<Integer> current, List<List<Integer>> result, int target){
if(target == 0){
result.add(new ArrayList<>(current));
return;
}
for(int i = index ; i < nums.length; i++){
if (i > index && nums[i] == nums[i - 1]) continue;
if(nums[i] > target){
break;
}
current.add(nums[i]);
backtrack(i+1, nums, current, result, target - nums[i]);
current.remove(current.size() - 1);
}
}
}