-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinationSumII.java
54 lines (42 loc) · 1.7 KB
/
CombinationSumII.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
import java.util.ArrayList;
import java.util.List;
public class CombinationSumII {
/**
*
* Given a collection of candidate numbers (candidates) and a target number (target),
* find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Time: O(2^n)
space: O(n)
*/
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(candidates, target, 0, new ArrayList<>(), res, 0);
return res;
}
public void helper(int[] candidates, int target, int cur, List<Integer> list, List<List<Integer>> res, int sum) {
if(sum == target) {
res.add(new ArrayList<>(list));
return;
}
if(sum < target) {
for(int i = cur; i < candidates.length; i++) {
if(i != cur && candidates[i] == candidates[i - 1]) continue;
//When it goes to next level, it could have duplicates, but not in the same level!!
sum += candidates[i];
list.add(candidates[i]);
helper(candidates, target, i + 1, list, res, sum);
list.remove(list.size() - 1);
sum -= candidates[i];
}
}
}
public static void main(String[] args) {
CombinationSumII a = new CombinationSumII();
int[] b = {10,1,2,7,6,1,5};
System.out.println(a.combinationSum2(b,8));
}
}