leetcode【中等】39、40、216、组合总和123

39、组合总和

在这里插入图片描述
思路1:回溯

在这里插入图片描述

  1. 搜索至任何一个节点时,总是会先判断当前节点是否可以通往最后的合法解。
  2. 如果不可以,则结束对「以当前节点为根节点的子树」的搜索,向父节点回溯,回到之前的状态,搜索下一个分支。
  3. 否则,进入该子树,继续以DFS的方式搜索。

注意:

  1. 空间树中的节点是动态的,即,当前有哪些选项可选择,是根据上一步的选择得出的,所以做回溯时,要把状态还原成进入当前节点之前的状态。
  2. 设置begin参数,即dfs是从当前节点向后搜索,避免重复
#减法
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        
        def dfs(begin,target):
            if target<0:
                return
            if target==0:
                res.append(path[:])
                return
            for i in range(begin,len(candidates)):
                path.append(candidates[i])
                dfs(i,target-candidates[i])
                path.pop()

        path=[]
        res=[]
        dfs(0,target)
        return res
class Solution {
    List<List<Integer>> res=new ArrayList<>();
    List<Integer>path=new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(candidates,target,0);
        return res;
    }
    public void dfs(int[] candidates, int target, int begin){
        if(target<0) return;
        if(target==0) {
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=begin;i<candidates.length;i++){
            path.add(candidates[i]);
            dfs(candidates,target-candidates[i],i);
            path.remove(path.size()-1);
        }

    }
}

40、组合总和II(无重复)

在这里插入图片描述
思路:回溯
总体和39一样,设置begin参数控制开始位置。避免重复,但本题一个组合内的同一个数不能重复使用,比如[1,2①,2②,5]target=8,按照39题应当是[[1,2①,5],[1,2②,5]],需要去重

解决方法:排序,使用begin跳过相同的数字

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        def dfs(begin,target):
        	if target<0:
        		return
            if target==0:
                ans.append(path[:])
                return
            for i in range(begin,len(candidates)):
                if i>begin and candidates[i]==candidates[i-1]:#跳过重复
                    continue
                path.append(candidates[i])
                dfs(i+1,target-candidates[i])
                path.pop()

        ans=[]
        path=[]
        candidates.sort()#排序
        dfs(0,target)
        return ans
class Solution {
    List<List<Integer>>res=new ArrayList<>();
    List<Integer>path=new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        dfs(candidates,target,0);
        return res;
    }
    public void dfs(int[] candidates, int target,int begin){
        if(target<0) return;
        if(target==0){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=begin;i<candidates.length;i++){
            if(i>begin && candidates[i]==candidates[i-1]){
                continue;
            }
            path.add(candidates[i]);
            dfs(candidates,target-candidates[i],i+1);
            path.remove(path.size()-1);
            
        }
    }

注:对于 [1,2,2,5] ,用i>begin and candidates[i]==candidates[i-1]跳过重复
在这里插入图片描述
例一不可以存在,例二可以。

即:同一层级不许重复,不同层级可以

candidates[i]==candidates[i-1]会把例一例二都砍掉,例二的第二个2没法出现

在一个for循环中,所有被遍历到的数都是属于一个层级的。我们要让一个层级中,必须出现且只出现一个2,那么就放过第一个出现重复的2,同时去掉后面的2。第一个出现的2的特点就是 cur == begin. 第二个出现的2 特点是cur > begin.

216. 组合总和 III(限制k个数)

在这里插入图片描述
dfs 时多加一个参数 k 记录当前还剩几个数,别的不变

class Solution {
    List<List<Integer>>res=new ArrayList<>();
    List<Integer>path=new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        dfs(k,n,1);
        return res;
    }
    public void dfs(int k,int n,int index){
        if(k==0){
            if(n==0) res.add(new ArrayList<>(path));            
            return;
        }
        for(int i=index;i<=Math.min(9,n);i++){
            path.add(i);
            dfs(k-1,n-i,i+1);
            path.remove(path.size()-1);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值