代码随想录算法训练营第20天

39. 组合总和

python 复制代码
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        path = []
        res = []
        def dfs(candidates,target,s,index):
            if s == target:
                res.append(path[:])
                return
            for i in range(index,len(candidates)):
                if s + candidates[i] > target:
                    break
                s += candidates[i]
                path.append(candidates[i])
                dfs(candidates,target,s,i)
                s -= candidates[i]
                path.pop()
        dfs(candidates,target,0,0)
        return res

40. 组合总和 II

python 复制代码
class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        used = [False for _ in range(len(candidates))]
        path = []
        res = []
        def dfs(candidates,used,target,s,index):
            if s == target:
                res.append(path[:])
                return
            for i in range(index,len(candidates)):
                if s + candidates[i] > target:
                    break
                if i > 0 and candidates[i] ==candidates[i-1] and used[i-1] == False:
                    continue
                s += candidates[i]
                used[i] = True
                path.append(candidates[i])
                dfs(candidates,used,target,s,i+1)
                s -= candidates[i]
                used[i] =False
                path.pop()
        dfs(candidates,used,target,0,0)
        return res

131. 分割回文串

python 复制代码
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        def isornot(s,left,right):
            if s[left:right+1] == s[left:right+1][::-1]:
                return True
            else:
                return False
        path = []
        res = []
        def dfs(s,index):
            if index ==len(s):
                res.append(path[:])
                return
            for i in range(index,len(s)):
                if isornot(s,index,i):
                    path.append(s[index:i+1])
                    dfs(s,i+1)
                    path.pop()
        dfs(s,0)
        return res

93. 复原 IP 地址

python 复制代码
class Solution:
    def isvalue(self,s,start,end):
        if start > end:
            return False
        if s[start] =='0' and start != end:
            return False
        if not 0 <=int(s[start:end+1]) <= 255:
            return False
        return True
    def restoreIpAddresses(self, s: str) -> List[str]:
        self.result = []
        self.dfs(s,0,0)
        return self.result
    def dfs(self,s,index,point_num):
        if point_num ==3:
            if self.isvalue(s,index,len(s)-1):
                self.result.append(s[:])
                return
        for i in range(index,len(s)):
            if self.isvalue(s,index,i):
                s = s[:i+1] + '.' +s[i+1:]
                point_num +=1
                self.dfs(s,i+2,point_num)
                point_num -=1
                s = s[:i+1] + s[i+2:]
            else:
                break

78. 子集

python 复制代码
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        path = []
        res = []
        def dfs(nums,index):
            res.append(path[:])
            if index > len(nums)-1:return
            for i in range(index,len(nums)):
                path.append(nums[i])
                dfs(nums,i+1)
                path.pop()
        dfs(nums,0)
        return res

90. 子集 II

python 复制代码
class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        used = [False for i in range(len(nums))]
        path =[]
        res = []
        def dfs(nums,used,index):
            res.append(path[:])
            if index == len(nums):
                return
            for i in range(index,len(nums)):
                if i > 0 and nums[i-1] == nums[i] and used[i-1] ==False:
                    continue
                path.append(nums[i])
                used[i] = True
                dfs(nums,used,i+1)
                used[i] = False
                path.pop()
        dfs(nums,used,0)
        return res

491. 非递减子序列

python 复制代码
class Solution:
    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        path = []
        res = []
        def dfs(nums,index):
            if len(path) > 1:
                res.append(path[:])
            # if index > len(nums)-1:return
            used = set()
            for i in range(index,len(nums)):
                if (path and path[-1] > nums[i]) or nums[i] in used:
                    continue
                used.add(nums[i])
                path.append(nums[i])
                dfs(nums,i+1)
                path.pop()
        dfs(nums,0)
        return res

46. 全排列

python 复制代码
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        path = []
        res = []
        used = [False for i in range(len(nums))]
        def dfs(nums,used):
            if len(path) == len(nums):
                res.append(path[:])
                return
            for i in range(len(nums)):
                if used[i] ==True:
                    continue
                path.append(nums[i])
                used[i] = True
                dfs(nums,used)
                path.pop()
                used[i] = False
        dfs(nums,used)
        return res

47. 全排列 II

python 复制代码
class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        used = [False for i in range(len(nums))]
        path = []
        res = []
        def dfs(nums,used):
            if len(path) ==len(nums):
                res.append(path[:])
                return
            for i in range(len(nums)):
                if i > 0 and nums[i-1] == nums[i] and used[i-1] == False:
                    continue
                if used[i] == True:
                    continue
                path.append(nums[i])
                used[i] = True
                dfs(nums,used)
                used[i] = False
                path.pop()
        dfs(nums,used)
        return res
相关推荐
云烟成雨TD22 分钟前
LangFlow 1.x 系列【3】入门案例
人工智能·python·agent
创世宇图24 分钟前
【Python工程化实战】Python 服务的结构化日志体系:structlog + JSON 输出 + 日志分级策略
python·elk·structlog·结构化日志·可观测性
创世宇图1 小时前
【Python工程化实战】Kubernetes 中 Python 应用的优雅启停与健康检查:零停机滚动更新实战
python·云原生·kubernetes·优雅停机
夜雪一千1 小时前
Python 使用OpenAI调用Qwen3.6-27B-ms模型|完整参数详解
开发语言·python
zhiSiBuYu05172 小时前
重排序(Rerank)提升检索准确率实战指南
开发语言·python·算法
MageGojo2 小时前
集成企业工商信息查询API:从在线调试到生产级调用实战
python·调试·rest api·api集成·企业信息查询
huangjiazhi_2 小时前
Python3.14编写文件服务器
python
郭梧悠2 小时前
算法:有效的括号
python·算法·leetcode
佛珠散了一地2 小时前
ONNX Runtime GPU 推理配置指南
python
派葛穆3 小时前
Python-pip切换镜像源
开发语言·python·pip