Question:

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.
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
  [1,2,2],
  [5]
]

Thinking:

  • Method:
    1. 参考39. Combination Sum.
    2. 因为同一个元素不能被使用两次,所以递归调用时,起始位置要跳过当前,需要+1。
    3. 原列表中是可以出现重复的,同一个位置相同元素只能出现一次,所以当我们加入新的元素后,要跳过其后的相同元素。
class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if(candidates == null || candidates.length == 0) return result;
        Arrays.sort(candidates);
        combinationSum2(candidates, target, new ArrayList<Integer>(), result, 0);
        return result;
    }
    public void combinationSum2(int[] candidates, int target, List<Integer> list, List<List<Integer>> result, int start){
        if(target == 0)
            result.add(new ArrayList<Integer>(list));
        else if(target < 0)
            return;
        else{
            int pre = -1;
            for(int i = start; i < candidates.length; i++){
                if(pre == -1)
                    pre = candidates[i];
                else if(candidates[i] == pre)
                    continue;
                else    pre = candidates[i];
                list.add(candidates[i]);
                combinationSum2(candidates, target - candidates[i], list, result, i + 1);
                list.remove(list.size() - 1);
            }
        }
    }
}

二刷

  1. 二刷还是借鉴了一刷的时候跳过元素的方法。
class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(candidates);
        backtrace(result, new ArrayList<Integer>(), candidates, target, 0, 0);
        return result;
    }
    private void backtrace(List<List<Integer>> result, List<Integer> temp, int[] candidates, int target, int sum, int start){
        if(sum == target){
            result.add(new ArrayList<>(temp));
        }else if(sum < target){
            int used = -1;
            for(int i = start; i < candidates.length; i++){
                if(sum + candidates[i] > target) return;
                if(used == -1) used = candidates[i];
                else if(used == candidates[i]) continue;
                else used = candidates[i];
                temp.add(candidates[i]);
                backtrace(result, temp, candidates, target, sum + candidates[i], i + 1);
                temp.remove(temp.size() - 1);
            }
        }
    }
}

Third time

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new LinkedList<>();
        if(candidates == null || candidates.length == 0) return result;
        Arrays.sort(candidates);
        dfs(result, candidates, target, 0, new LinkedList<>(), 0);
        return result;
    }
    private void dfs(List<List<Integer>> result, int[] candidates, int target, int sum, List<Integer> temp, int index){
        if(sum == target) result.add(new LinkedList<Integer>(temp));
        else if(sum < target){
            for(int i = index; i < candidates.length; i++){
                if(sum + candidates[i] > target) continue;
                while(i < candidates.length && i > index && candidates[i - 1] == candidates[i]) i++;
                if(i >= candidates.length) break;
                temp.add(candidates[i]);
                dfs(result, candidates, target, sum + candidates[i], temp, i + 1);
                temp.remove(temp.size() - 1);
            }
        }
    }
}