代码随想录 Day-27|#39 组合总和|#40 组合总和II|#131 分割回文串

清单

● 39. 组合总和
● 40.组合总和II
● 131.分割回文串

LeetCode #39 组合总和

1. 题目

给你一个无重复元素的整数数组 candidates 和一个目标整数 target,找出 candidates 中可以使数字和为目标数 target 的所有不同组合,并以列表形式返回。你可以按任意顺序返回这些组合。
candidates 中的同一个数字可以无限制重复被选取。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。

2. 思路

按照回溯三部曲:

  1. 递归函数的返回值和参数, 整数数组 candidates,目标整数 target,遍历路径path,以及储存结果result
  2. 终止条件:当path的累积和等同于target时,将path导入result,返回
  3. 单层搜索过程: for循环遍历

3. 代码实现

初始代码运行后,发现path中存在重复项, 添加startindex后解决

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        self.backtracing(candidates,target, 0, [],result)
        return result
    
    def backtracing(self, candidates, target, start, path, result):
        if sum(path) == target:
            result.append(path[:])

        if sum(path) > target:
            return

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

LeetCode #40 组合总和II

1. 题目

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。

2. 思路

按照回溯三部曲:

  1. 递归函数的返回值和参数, 整数数组 candidates,目标整数 target,遍历路径path,以及储存结果result
  2. 终止条件:当path的累积和等同于target时,将path导入result,返回
  3. 单层搜索过程: for循环遍历

3. 代码实现

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        candidates.sort()
        self.backtracing(candidates, target, 0, [], result)
        return result

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

        if sum(path) > target:
            return
        
        for i in range(startIndex, len(candidates)):
            if i > startIndex and candidates[i] == candidates[i-1]: #去重
                continue

            path.append(candidates[i])
            self.backtracing(candidates, target, i+1, path, result)
            path.pop()

LeetCode #131 分割回文串

1. 题目

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文串。返回 s 所有可能的分割方案。回文串是正着读和反着读都一样的字符串。

2. 思路

按照回溯三部曲:

  1. 递归函数的返回值和参数, 字符串s,起始值startindex,遍历路径path,以及储存结果result
  2. 终止条件:当startIndex等同于字符串长度时,将path导入result,返回
  3. 单层搜索过程: for循环遍历

3. 代码实现

class Solution:
    def partition(self, s: str) -> List[List[str]]:
        result = []
        self.backtracing(s, 0, [],result)
        return result
    
    def backtracing(self, s, startIndex, path, result):
        if startIndex == len(s):
            result.append(path[:])
            return
        
        for i in range(startIndex, len(s)):        
            if s[startIndex: i + 1] == s[startIndex: i + 1][::-1]:
                path.append(s[startIndex: i + 1])
                self.backtracing(s, i + 1, path, result)
                path.pop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值