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


    39. 组合总和

    1. class Solution:
    2. def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
    3. candidates.sort()
    4. path = []
    5. res = []
    6. def dfs(candidates,target,s,index):
    7. if s == target:
    8. res.append(path[:])
    9. return
    10. for i in range(index,len(candidates)):
    11. if s + candidates[i] > target:
    12. break
    13. s += candidates[i]
    14. path.append(candidates[i])
    15. dfs(candidates,target,s,i)
    16. s -= candidates[i]
    17. path.pop()
    18. dfs(candidates,target,0,0)
    19. return res

    40. 组合总和 II

    1. class Solution:
    2. def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
    3. candidates.sort()
    4. used = [False for _ in range(len(candidates))]
    5. path = []
    6. res = []
    7. def dfs(candidates,used,target,s,index):
    8. if s == target:
    9. res.append(path[:])
    10. return
    11. for i in range(index,len(candidates)):
    12. if s + candidates[i] > target:
    13. break
    14. if i > 0 and candidates[i] ==candidates[i-1] and used[i-1] == False:
    15. continue
    16. s += candidates[i]
    17. used[i] = True
    18. path.append(candidates[i])
    19. dfs(candidates,used,target,s,i+1)
    20. s -= candidates[i]
    21. used[i] =False
    22. path.pop()
    23. dfs(candidates,used,target,0,0)
    24. return res

    131. 分割回文串

    1. class Solution:
    2. def partition(self, s: str) -> List[List[str]]:
    3. def isornot(s,left,right):
    4. if s[left:right+1] == s[left:right+1][::-1]:
    5. return True
    6. else:
    7. return False
    8. path = []
    9. res = []
    10. def dfs(s,index):
    11. if index ==len(s):
    12. res.append(path[:])
    13. return
    14. for i in range(index,len(s)):
    15. if isornot(s,index,i):
    16. path.append(s[index:i+1])
    17. dfs(s,i+1)
    18. path.pop()
    19. dfs(s,0)
    20. return res

    93. 复原 IP 地址

    1. class Solution:
    2. def isvalue(self,s,start,end):
    3. if start > end:
    4. return False
    5. if s[start] =='0' and start != end:
    6. return False
    7. if not 0 <=int(s[start:end+1]) <= 255:
    8. return False
    9. return True
    10. def restoreIpAddresses(self, s: str) -> List[str]:
    11. self.result = []
    12. self.dfs(s,0,0)
    13. return self.result
    14. def dfs(self,s,index,point_num):
    15. if point_num ==3:
    16. if self.isvalue(s,index,len(s)-1):
    17. self.result.append(s[:])
    18. return
    19. for i in range(index,len(s)):
    20. if self.isvalue(s,index,i):
    21. s = s[:i+1] + '.' +s[i+1:]
    22. point_num +=1
    23. self.dfs(s,i+2,point_num)
    24. point_num -=1
    25. s = s[:i+1] + s[i+2:]
    26. else:
    27. break

    78. 子集

    1. class Solution:
    2. def subsets(self, nums: List[int]) -> List[List[int]]:
    3. path = []
    4. res = []
    5. def dfs(nums,index):
    6. res.append(path[:])
    7. if index > len(nums)-1:return
    8. for i in range(index,len(nums)):
    9. path.append(nums[i])
    10. dfs(nums,i+1)
    11. path.pop()
    12. dfs(nums,0)
    13. return res

    90. 子集 II

    1. class Solution:
    2. def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
    3. nums.sort()
    4. used = [False for i in range(len(nums))]
    5. path =[]
    6. res = []
    7. def dfs(nums,used,index):
    8. res.append(path[:])
    9. if index == len(nums):
    10. return
    11. for i in range(index,len(nums)):
    12. if i > 0 and nums[i-1] == nums[i] and used[i-1] ==False:
    13. continue
    14. path.append(nums[i])
    15. used[i] = True
    16. dfs(nums,used,i+1)
    17. used[i] = False
    18. path.pop()
    19. dfs(nums,used,0)
    20. return res

    491. 非递减子序列
     

    1. class Solution:
    2. def findSubsequences(self, nums: List[int]) -> List[List[int]]:
    3. path = []
    4. res = []
    5. def dfs(nums,index):
    6. if len(path) > 1:
    7. res.append(path[:])
    8. # if index > len(nums)-1:return
    9. used = set()
    10. for i in range(index,len(nums)):
    11. if (path and path[-1] > nums[i]) or nums[i] in used:
    12. continue
    13. used.add(nums[i])
    14. path.append(nums[i])
    15. dfs(nums,i+1)
    16. path.pop()
    17. dfs(nums,0)
    18. return res

    46. 全排列

    1. class Solution:
    2. def permute(self, nums: List[int]) -> List[List[int]]:
    3. path = []
    4. res = []
    5. used = [False for i in range(len(nums))]
    6. def dfs(nums,used):
    7. if len(path) == len(nums):
    8. res.append(path[:])
    9. return
    10. for i in range(len(nums)):
    11. if used[i] ==True:
    12. continue
    13. path.append(nums[i])
    14. used[i] = True
    15. dfs(nums,used)
    16. path.pop()
    17. used[i] = False
    18. dfs(nums,used)
    19. return res

    47. 全排列 II

    1. class Solution:
    2. def permuteUnique(self, nums: List[int]) -> List[List[int]]:
    3. nums.sort()
    4. used = [False for i in range(len(nums))]
    5. path = []
    6. res = []
    7. def dfs(nums,used):
    8. if len(path) ==len(nums):
    9. res.append(path[:])
    10. return
    11. for i in range(len(nums)):
    12. if i > 0 and nums[i-1] == nums[i] and used[i-1] == False:
    13. continue
    14. if used[i] == True:
    15. continue
    16. path.append(nums[i])
    17. used[i] = True
    18. dfs(nums,used)
    19. used[i] = False
    20. path.pop()
    21. dfs(nums,used)
    22. return res

  • 相关阅读:
    Rust机器学习之ndarray
    spring ExpressionParser 四则运算表达式解析参数提取
    直播笔记 | 散养职业故事:敏捷教练送外卖
    springboot+mybatis-plus+element ui生成二维码
    基于 LSTM 进行多类文本分类(附源码)
    ARM-A架构入门基础(二)异常处理
    Vue中如何进行表格排序与过滤
    如何实现前端缓存管理?
    SpringBoot3基础:最简项目示例
    5. Makefile项目管理
  • 原文地址:https://blog.csdn.net/weixin_44733295/article/details/136456797