|
1 | 1 | M |
| 2 | +1526759152 |
| 3 | +tags: Array, DFS, Backtracking, Combination |
| 4 | + |
| 5 | +给一串数字candidates (can have duplicates), 和一个target. |
| 6 | + |
| 7 | +找到所有unique的 组合(combination) int[], 要求每个combination的和 = target. |
| 8 | + |
| 9 | +注意: 同一个candidate integer, 只可以用一次. |
| 10 | + |
| 11 | +#### DFS, Backtracking |
| 12 | +- when the input has duplicates, and want to skip redundant items? |
| 13 | +- 1. sort. 2. in for loop, skip same neighbor. |
| 14 | +- 考虑input: 有duplicate, 必须sort |
| 15 | +- 考虑重复使用的规则: 不可以重复使用 |
| 16 | +- 1. for loop里面dfs的时候, 使用curr index + 1 |
| 17 | +- 2. for loop里面, 同一个level, 同一个数字, 不能重复使用: `(i > index && candidates[i] == candidates[i - 1]) continue` |
| 18 | +- 因为在同一个level里面重复的数字在下一个dfs level里面是会被考虑到的, 这里必须skip (这个就记住吧) |
| 19 | +- the result is trivial, save success list into result. |
2 | 20 |
|
3 | | -还是DFS. 和Combination Sum I 类似. |
4 | | -确保Helper是用i+1,下一层的数字, 不允许重复。 |
5 | 21 |
|
6 | 22 | ``` |
7 | 23 | /* |
| 24 | +Given a collection of candidate numbers (candidates) and a target number (target), |
| 25 | +find all unique combinations in candidates where the candidate numbers sums to target. |
| 26 | +
|
| 27 | +Each number in candidates may only be used once in the combination. |
| 28 | +
|
| 29 | +Note: |
| 30 | +
|
| 31 | +All numbers (including target) will be positive integers. |
| 32 | +The solution set must not contain duplicate combinations. |
| 33 | +Example 1: |
| 34 | +
|
| 35 | +Input: candidates = [10,1,2,7,6,1,5], target = 8, |
| 36 | +A solution set is: |
| 37 | +[ |
| 38 | + [1, 7], |
| 39 | + [1, 2, 5], |
| 40 | + [2, 6], |
| 41 | + [1, 1, 6] |
| 42 | +] |
| 43 | +Example 2: |
| 44 | +
|
| 45 | +Input: candidates = [2,5,2,1,2], target = 5, |
| 46 | +A solution set is: |
| 47 | +[ |
| 48 | + [1,2,2], |
| 49 | + [5] |
| 50 | +] |
| 51 | +
|
| 52 | +
|
| 53 | + */ |
| 54 | + |
| 55 | +/* |
| 56 | +- one item can be picked once. the input candidates may have duplicates. |
| 57 | +- IMPORTANT: 1. sort. 2. Skip adjacent item in for loop |
| 58 | +- use dfs, for loop to aggregate candidates |
| 59 | +- do target - val to track, and when target == 0, that’s a solution |
| 60 | +- dfs(curr index i), instead of (i + 1): allows reuse of item |
| 61 | +
|
| 62 | +*/ |
| 63 | +class Solution { |
| 64 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 65 | + List<List<Integer>> result = new ArrayList<>(); |
| 66 | + // check edge case |
| 67 | + if (candidates == null || candidates.length == 0 || target <= 0) { |
| 68 | + return result; |
| 69 | + } |
| 70 | + Arrays.sort(candidates); // critical to skip duplicates |
| 71 | + // init reuslt, dfs |
| 72 | + dfs(result, new ArrayList<>(), candidates, 0, target); |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + private void dfs(List<List<Integer>> result, List<Integer> list, |
| 77 | + int[] candidates, int index, int target) { |
| 78 | + // for loop, where dfs is performed |
| 79 | + for (int i = index; i < candidates.length; i++) { |
| 80 | + // ensures at same for loop round, the same item (sorted && neighbor) won't be picked 2 times |
| 81 | + if (i > index && candidates[i] == candidates[i - 1]) continue; |
| 82 | + |
| 83 | + int value = candidates[i]; |
| 84 | + list.add(value); |
| 85 | + if (target == value) { |
| 86 | + result.add(new ArrayList<>(list)); |
| 87 | + } else (target - value > 0) { |
| 88 | + dfs(result, list, candidates, i + 1, target - value); |
| 89 | + } |
| 90 | + list.remove(list.size() - 1); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +/* |
| 98 | +LincCode |
8 | 99 | Given a collection of candidate numbers (C) and a target number (T), |
9 | 100 | find all unique combinations in C where the candidate numbers sums to T. |
10 | 101 |
|
|
0 commit comments