代码随想录算法训练营第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
相关推荐
网易独家音乐人Mike Zhou2 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
安静读书2 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频
小二·4 小时前
java基础面试题笔记(基础篇)
java·笔记·python
小喵要摸鱼5 小时前
Python 神经网络项目常用语法
python
一念之坤6 小时前
零基础学Python之数据结构 -- 01篇
数据结构·python
wxl7812277 小时前
如何使用本地大模型做数据分析
python·数据挖掘·数据分析·代码解释器
NoneCoder7 小时前
Python入门(12)--数据处理
开发语言·python
LKID体8 小时前
Python操作neo4j库py2neo使用(一)
python·oracle·neo4j
小尤笔记8 小时前
利用Python编写简单登录系统
开发语言·python·数据分析·python基础
FreedomLeo18 小时前
Python数据分析NumPy和pandas(四十、Python 中的建模库statsmodels 和 scikit-learn)
python·机器学习·数据分析·scikit-learn·statsmodels·numpy和pandas