-
Notifications
You must be signed in to change notification settings - Fork 5
/
CombinationSum2.java
63 lines (60 loc) · 2.45 KB
/
CombinationSum2.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*https://leetcode.com/problems/combination-sum-ii/*/
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
//marker per path
boolean[] used = new boolean[candidates.length];
backtracking(ans, new ArrayList<Integer>(), candidates, target, 0, 0, used);
return ans;
}
private void backtracking(List<List<Integer>> ans, List<Integer> path, int[] candidates, int target, int sum, int index, boolean[] used) {
if (sum == target)
{
//make a new copy of the current path, as path will change throughout the recursion
ans.add(new ArrayList<Integer>(path));
return;
}
for (int i = index; i < candidates.length; i++)
{
//early pruning
if (sum + candidates[i] > target) break;
//avoid duplicates at same tree depth, allowing duplicate at different tree depths (eg. [1,1,1,2] target:3; allows [1,1,1], not allow [1,2] after [1,2])
if (i > 0 && candidates[i] == candidates[i-1] && !used[i-1]) continue;
path.add(candidates[i]);
used[i] = true;
backtracking(ans, path, candidates, target, sum + candidates[i], i + 1, used);
used[i] = false;
path.remove(path.size() - 1);
}
}
}
class Solution {
List<List<Integer>> resultSet;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
resultSet = new ArrayList<List<Integer>>();
generateCombinations(candidates,target,-1,0,new ArrayList<Integer>());
return resultSet;
}
private void generateCombinations(int[] candidates, int target, int index, int sum, List<Integer> curr)
{
if (sum == target)
{
List<Integer> newCombination = new ArrayList<>(curr);
Collections.sort(newCombination);
resultSet.add(newCombination);
return;
}
for (int i = index+1; i < candidates.length; ++i)
{
if (i > index+1 && candidates[i] == candidates[i-1]) continue;
if (sum+candidates[i] <= target)
{
curr.add(candidates[i]);
generateCombinations(candidates,target,i,sum+candidates[i],curr);
curr.remove(curr.size()-1);
}
}
}
}