代码随想录算法训练营第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
相关推荐
梧桐树04291 小时前
python常用内建模块:collections
python
Dream_Snowar1 小时前
速通Python 第三节
开发语言·python
蓝天星空2 小时前
Python调用open ai接口
人工智能·python
jasmine s3 小时前
Pandas
开发语言·python
郭wes代码3 小时前
Cmd命令大全(万字详细版)
python·算法·小程序
leaf_leaves_leaf3 小时前
win11用一条命令给anaconda环境安装GPU版本pytorch,并检查是否为GPU版本
人工智能·pytorch·python
夜雨飘零13 小时前
基于Pytorch实现的说话人日志(说话人分离)
人工智能·pytorch·python·声纹识别·说话人分离·说话人日志
404NooFound3 小时前
Python轻量级NoSQL数据库TinyDB
开发语言·python·nosql
天天要nx3 小时前
D102【python 接口自动化学习】- pytest进阶之fixture用法
python·pytest
minstbe3 小时前
AI开发:使用支持向量机(SVM)进行文本情感分析训练 - Python
人工智能·python·支持向量机