代码随想录算法训练营day27|39. 组合总和、40.组合总和II、131.分割回文串

39. 组合总和 

如下树形结构如下:

39.组合总和

选取第二个数字5之后,剩下的数字要从5、3中取数了,不能再取2了,负责组合就重复了,注意这一点,自己做的时候没想明白这一点

如果是一个集合来求组合的话,就需要startIndex,例如:77.组合 (opens new window)216.组合总和III

如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:17.电话号码的字母组合

代码如下:

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result= []
        path = []
        self.backtracking(candidates,0,target,path,result)
        return result

    def backtracking(self,candidates,startIndex,target,path,result):
        if sum(path) > target:
            return
        if sum(path) == target:
            result.append(path[:])
            return

        for i in range(startIndex,len(candidates)):
            path.append(candidates[i])
            self.backtracking(candidates,i,target,path,result)
            path.pop()
        

剪枝优化

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result= []
        path = []
        candidates.sort() #列表要排序
        self.backtracking(candidates,0,target,path,result)
        return result

    def backtracking(self,candidates,startIndex,target,path,result):
       
        if sum(path) == target:
            result.append(path[:])
            return

        for i in range(startIndex,len(candidates)): #剪枝优化都是在for循环中做优化
            if sum(path) > target: #如果组合的和比目标值要大就跳出循环
                break 
            path.append(candidates[i])
            self.backtracking(candidates,i,target,path,result)
            path.pop()
        

 40.组合总和II 

自己做法:

考虑到把数组排序了,那么在收集数组的时候,也是按照排序来的,所以可以直接return结果:

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        candidates.sort()
        self.backtrack(candidates,target,0,[],result)
        return result
    
    def backtrack(self,candidates,target,startIndex,path,result):
        if path in result: #直接return
            return
        if sum(path) == target:
            result.append(path[:])
            return
        
        for i in range(startIndex,len(candidates)):
            if sum(path) > target:
                continue
            path.append(candidates[i])
            self.backtrack(candidates,target,i+1,path,result)
            path.pop()        

代码随想录:

class Solution:


    def backtracking(self, candidates, target, total, startIndex, path, result):
        if total == target:
            result.append(path[:])
            return

        for i in range(startIndex, len(candidates)):
            if i > startIndex and candidates[i] == candidates[i - 1]:
                continue

            if total + candidates[i] > target:
                break

            total += candidates[i]
            path.append(candidates[i])
            self.backtracking(candidates, target, total, i + 1, path, result)
            total -= candidates[i]
            path.pop()

    def combinationSum2(self, candidates, target):
        result = []
        candidates.sort()
        self.backtracking(candidates, target, 0, 0, [], result)
        return result

使用used:

选择过程树形结构如图所示:

40.组合总和II

class Solution:


    def backtracking(self, candidates, target, total, startIndex, used, path, result):
        if total == target:
            result.append(path[:])
            return

        for i in range(startIndex, len(candidates)):
            # 对于相同的数字,只选择第一个未被使用的数字,跳过其他相同数字
            if i > startIndex and candidates[i] == candidates[i - 1] and not used[i - 1]:
                continue

            if total + candidates[i] > target:
                break

            total += candidates[i]
            path.append(candidates[i])
            used[i] = True
            self.backtracking(candidates, target, total, i + 1, used, path, result)
            used[i] = False
            total -= candidates[i]
            path.pop()

    def combinationSum2(self, candidates, target):
        used = [False] * len(candidates)
        result = []
        candidates.sort()
        self.backtracking(candidates, target, 0, 0, used, [], result)
        return result

这道题没理解 

class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        res = []
        path = []

        def backtrack(s, start_index):
            if start_index >= len(s): # 终止条件
                res.append(path[:])
                return

            for i in range(start_index, len(s)):
                string = s[start_index:i+1]
                if string == string[::-1]: # 并不是每一个位置的i都满足要求,满足要求的i才继续切割
                    path.append(string)
                    backtrack(s, i+1) # 要从下一个位置开始切割
                    path.pop()
                else:
                    continue

        backtrack(s, 0)
        return res

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值