leetcode之组合数(Combination Sum)

Combination Sum

//代码1
public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length == 0 || candidates == null) return result;
        Arrays.sort(candidates);
        List<Integer> list = new ArrayList<Integer>();
        helper(result, list, candidates, 0, target);
        return result;
    }

    private void helper(List<List<Integer>> result,
                        List<Integer> list,
                        int[] candidates,
                        int sum,
                        int target) {
        if(sum == target) {
            result.add(new ArrayList<Integer>(list));
            return;
        }

        for(int i = 0; i < candidates.length; i++) {//每层的循环都是从0开始,导致出现重复的组合
            if(sum + candidates[i] <= target) {
                list.add(candidates[i]);
                helper(result, list, candidates, sum + candidates[i], target);
                list.remove(list.size() - 1);
            }
            else
                break;
        }
    }
}

Your answer:[[2,2,3],[2,3,2],[3,2,2],[7]]
Expected answer:[[2,2,3],[7]]

//代码2
public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length == 0 || candidates == null) return result;
        Arrays.sort(candidates);
        List<Integer> list = new ArrayList<Integer>();
        helper(result, list, candidates, target, 0);
        return result;
    }

    private void helper(List<List<Integer>> result,
                        List<Integer> list,
                        int[] candidates,
                        int target,
                        int k) {
        if(target == 0) {
            result.add(new ArrayList<Integer>(list));
            return;
        }
        if(target > 0) {
            for(int i = k; i < candidates.length; i++) {
                if(target < candidates[i]) break;
                list.add(candidates[i]);
                helper(result, list, candidates, target - candidates[i], i);//两处修改的地方
                list.remove(list.size() - 1);
            }
        }
    }
}

正解代码2对代码1做了2点优化:

  1. 每层循环从当前开始层数开始,防止出现重复组合;
  2. 传入的参数由和改为差值,这样可防止在数值很大的情况下出现溢出的异常。

Combination Sum II

在回溯法中,如果要去除重复的元素,只需在for循环中加入一个条件判断即可,这个条件判断隐含了两层意思:
1. 每一层的第一个元素无条件加入解空间树
2. 这一层从第二个元素开始进行比较,若这个元素与前面元素相同,不应该加入解空间树

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length == 0 || candidates == null) return result;
        Arrays.sort(candidates);
        List<Integer> list = new ArrayList<Integer>();
        helper(result, list, candidates, target, 0);
        return result;
    }

    private void helper(List<List<Integer>> result,
                        List<Integer> list,
                        int[] candidates,
                        int target,
                        int k) {
        if(target == 0) {
            result.add(new ArrayList<Integer>(list));
            return;
        }
        if(target > 0) {
            for(int i = k; i < candidates.length; i++) {
                if(target < candidates[i]) break;
                if(i > k && candidates[i] == candidates[i - 1]) continue;//多个一个判断条件
                list.add(candidates[i]);
                helper(result, list, candidates, target - candidates[i], i + 1);//在代码2中,这里是i
                list.remove(list.size() - 1);
            }
        }
    }
}

转载于:https://www.cnblogs.com/season-peng/p/6713497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值