代码随想录算法训练营第二十二天| 39. 组合总和、40.组合总和II、17.电话号码的字母组合

题目链接:39. 组合总和 - 力扣(LeetCode)

思路:每次回溯的时候同样的数字能被取到,所以更改一下startIndex即可

class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """

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

            if sum(self.path) == target:
                self.result.append(self.path[:])
                return

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

题目链接:40. 组合总和 II - 力扣(LeetCode)

思路:主要是要在树的宽度上去重,而树的高度上不用去重,需要用一个used数组来进行判断

class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        self.result = []
        self.path = []
        self.used = [False] * len(candidates)
        candidates.sort()
        def backTracking(startIndex, candidates, target):
            if sum(self.path) > target:
                return
            if sum(self.path) == target:
                self.result.append(self.path[:])                
                return

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

                self.path.append(candidates[i])
                self.used[i] = True
                backTracking(i + 1, candidates, target)
                self.used[i] = False
                self.path.pop()
        
        backTracking(0, candidates, target)
        return self.result

题目链接:131. 分割回文串 - 力扣(LeetCode)

class Solution(object):
    def isHuiWen(self,s , startIndex, end):
        i = startIndex
        j = end
        while i < j:
            if s[i] != s[j]:
                return False
            i = i + 1
            j = j - 1
        return True

    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """

        self.result = []
        self.path = []

        def backTracking(startIndex):
            if startIndex == len(s):
                self.result.append(self.path[:])
                return
            #[aab]
            for i in range(startIndex, len(s)):# 0, 1, 2 | 1, 2 | 2 | 3 == len(s) return
                if self.isHuiWen(s, startIndex, i):# 0, 0 -> a | 1, 1-> a | 2, 2->b
                    self.path.append(s[startIndex : i+1]) # ["a"] ["a"] ["b"]
                    backTracking(i+1)
                    self.path.pop()

        backTracking(0)

        return self.result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值