回溯-39.数组总和,40.数组总和II, 216.数组总和III

leetcode中关于回溯法的数组总和的一系列题目

39.数组总和

题目链接

在这里插入图片描述

回溯的做法:

题目要求的是组合,那就是说[2,2,3]和[2,3,2]是一种情况,并且candidates中的元素可以重复选择,所以在回溯的过程中,下一次递归的起始位置就跟本次递归的起始位置一样。每一次递归区间总是[index...candidates.length],在解空间树中就是与该节点的子分支的个数,并不会选择自己前边的数所以就不会出现[i+1,i]这种排列,这样就解决了组合的问题

最后就是确定递归的出口,根据题意选择的子集的和等于target才能加入到结果集ans中,所以每一次决策给targer减去选择的值,知道target为0时就可以把path添加到结果集中,并返回上一层继续试探决策。如果target小于0我们就没有在试探下去的必要,所以当target小于0时直接返回到上一层。

class Solution {
    //结果集
    List<List<Integer>> ans=new ArrayList<List<Integer>>();
    //子结果,每一次决策的结果
    List<Integer> path=new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(target,candidates,0);
        return ans;
    }
    public void backtracking(int target,int[] candidates,int index){
        //没有试探下去的必要
        if(target<0)return;

        //满足条件添加到结果集
        if(target==0){
            ans.add(new ArrayList<>(path));
            return;
        }
        
        for(int i=index;i<candidates.length;i++){
            path.add(candidates[i]);//添加决策
            backtracking(target-candidates[i],candidates,i);
            path.remove(path.size()-1);//回溯
        }
    }
}

40.数组总和II

题目链接

在这里插入图片描述

回溯:

这到题目与39.数组总和很相似,为一的区别是每个数字只能使用一次,且不能有重复的组合。每个数字只使用一次很好解决,只是下一次递归开始的时候总下一个位置开始就行。

至于不能有重复的组合,举个例子集合s[1,2,1]target=3,选了第一个1就不能选第二个1因为这两个的组合都是[1,2]是不允许的,而39题则是允许存在的。所以这里就要去重,如果画一下解空间树就会发现重复是在每一层中出现的所以就要在for循环中去重(递归是向下生成孩子,循环是向右生成分支)。每一层去重分支就很简单,我前边的选了x,那我就不能选x就很数组去重一样:如果candidates[i]==candidates[i-1]就跳过。但是前提是需要排完序后再去重。

class Solution {
    List<List<Integer>>ans=new ArrayList<List<Integer>>();
    List<Integer>path=new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates,target,0);
        return ans;
    }
    public void backtracking(int[] candidates,int target,int index){
        if(target<0)return;
        if(target==0){
            ans.add(new ArrayList<>(path));
            return;
        }
        for(int i=index;i<candidates.length;i++){
            //这里与数组总和不同,i>index是为了保证选择是有效的
            if(i>index&&candidates[i]==candidates[i-1]){
                continue;
            }
            path.add(candidates[i]);
            backtracking(candidates,target-candidates[i],i+1);//这里i+1,因为每个元素只能选择一次
            path.remove(path.size()-1);
        }
    }
}

216.数组总和III

题目链接

在这里插入图片描述

题解:
这到题比39.数组总和多了一个条件,限制了path的长度,所以我们只需要在出口处做改变就好了

class Solution {
    List<List<Integer>>ans=new ArrayList<List<Integer>>();
    List<Integer>path=new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        backtracking(k,n,1);
        return ans;
    }
    public void backtracking(int k,int n,int index){
        //出口
        //继续试探没有必要
        if(path.size()>k||n<0)return;

        if(path.size()==k&&n==0){
            ans.add(new ArrayList(path));
            return;
        }
        
        for(int i=index;i<=9;i++){
            path.add(i);
            backtracking(k,n-i,i+1);//不能重复
            path.remove(path.size()-1);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值