Given a set of candidate numbers (C)(without duplicates)and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set[2, 3, 6, 7]and target7,
A solution set is:

[
  [7],
  [2, 2, 3]
]

Solution: 每层recursion一种数字,注意加了多少个就要删去多少个

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        if (candidates == null || candidates.length == 0) {
            return ans;
        }
        helper(0, 0, target, new ArrayList<Integer>(), ans, candidates);
        return ans;
    }
    private void helper(int level, int sum, int target, ArrayList<Integer> path, List<List<Integer>> ans, int[] candidates) {
        if (level >= candidates.length) {
            return;
        }
        int last = path.size();
        while (sum < target) {
            helper(level + 1, sum, target, path, ans, candidates);
            path.add(candidates[level]);
            sum += candidates[level];
            if (sum == target) {
                ans.add(new ArrayList<>(path));
            }
        }
        while (path.size() > last) {
            path.remove(path.size() - 1);
        }
    }

Follow up: 如果有duplicates, 每个数可用无限次,则先sort array,只用第一次出现的数

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // write your code here
        List<List<Integer>> result = new ArrayList<>();
        if(candidates == null || candidates.length == 0){
            return result;
        }
        List<Integer> sum = new ArrayList<>();
        Arrays.sort(candidates);
        dfs(result, sum, candidates, target, 0);
        return result;

    }

    private void dfs(List<List<Integer>> result, List<Integer> sum, int[] candidates, int target, int start){
        if(target == 0){

            result.add(new ArrayList<Integer>(sum));
            return;
        }
        for(int i = start; i < candidates.length; i++){
            if(target - candidates[i] < 0){
                break;
            }
            if(i - 1 >= 0 && candidates[i] == candidates[i - 1]){
                continue;
            }
            sum.add(candidates[i]);
                                                            //注意是i
            dfs(result, sum, candidates, target - candidates[i], i);
            sum.remove(sum.size() - 1);
        }
        return;

    }

Follow up: 如果有duplicates, 每个数只能用一次,则先sort array,每层recursion用index开始以后的数,只用index后第一次出现的数。注意与上一题的比较。

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        if (candidates == null || candidates.length == 0) {
            return ans;
        }
        Arrays.sort(candidates);
        helper(0, 0, target, new ArrayList<Integer>(), ans, candidates);
        return ans;
    }
    private void helper(int index, int sum, int target, ArrayList<Integer> path, List<List<Integer>> ans, int[] candidates) {
        if(target == sum){            
            ans.add(new ArrayList<Integer>(path));
            return;
        }

        for(int i = index; i < candidates.length; i++){
            //注意不是i - 1 >= 0
            if(i - 1 >= index && candidates[i] == candidates[i - 1]){
                continue;
            }
            sum += candidates[i];
            if(sum > target){
                break;
            }            
            path.add(candidates[i]);
            //注意是i + 1
            helper(i + 1, sum, target, path, ans, candidates);
            path.remove(path.size() - 1);
            sum -= candidates[i];
        }
    }

results matching ""

    No results matching ""