代码随想录算法训练营第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
相关推荐
GPU实战笔记1 小时前
云端 Python 开发:JupyterLab 还是 VS Code Remote-SSH?
python·vs code·jupyterlab·remote-ssh·远程开发
狗狗狗狗狗乐啊1 小时前
搭一个 AI 对话工作台 AChat:从 0 到可用的完整记录(一)
python·react.js·ai编程
白猫不黑2 小时前
Python实现简易Web弱口令爆破与防护方案
python·web安全·计算机·网络安全·黑客·信息安全·渗透测试
kuuailetianzi2 小时前
Python初识:定位、优势与发展历程
python
guoran_shini3 小时前
LLM微调-训练垂类问答模型
python·lora·sft
basketball6163 小时前
Python FastAPI 介绍以及常用方法
python·fastapi·vllm·ai infra
论文复现现场3 小时前
企业知识库 RAG 用什么模型便宜?GLM-5.3-Flash 长文本 API 实测
python·rag·企业知识库·大模型api·glm-5.3-flash
梦在远山后3 小时前
从手写 Loop 到可恢复 Runtime:用 LangGraph、PostgreSQL Checkpoint 与 AG-UI 跑通中断恢复
python·langchain·agent
Yolanda_20224 小时前
8.tensorboard的使用
python
用户0332126663674 小时前
使用 Python 添加、隐藏或删除 PowerPoint 幻灯片
python