当前位置: 首页 > news >正文

算法记录 | Day27 回溯算法

39.组合总和

思路:

1.确定回溯函数参数:定义全局遍历存放res集合和单个path,还需要

  • candidates数组

  • targetSum(int)目标和。

  • startIndex(int)为下一层for循环搜索的起始位置。

2.终止条件:

  • 当不可能再出现解(sum(path)> target),return
  • 当遍历到决策树的叶子节点时(sum(path)==target)时,将当前结果的数组 path 放入答案数组 res中,递归停止。

3.遍历过程:数组可以重复,startindex从i开始

  • 从当前正在考虑元素,到数组结束为止,枚举出所有可选的元素。对于每一个可选元素:
    • 选择元素:将其添加到当前数组 path 中。
    • 递归搜索:在选择该元素的情况下,继续递归选择剩下元素。
    • 撤销选择:将该元素从当前结果数组 path 中移除。
class Solution:def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:res = []path = []def backtrack(candidates,target,startindex):if sum(path) > target:return if sum(path) == target:return res.append(path[:])for i in range(startindex,len(candidates)):path.append(candidates[i])backtrack(candidates,target,i)path.pop()backtrack(candidates, target,0)return res

40. 组合总和 II

思路:

1.确定回溯函数参数:定义全局遍历存放res集合和单个path,还需要

  • candidates数组

  • targetSum(int)目标和。

  • startIndex(int)为下一层for循环搜索的起始位置。

2.终止条件:

  • 当不可能再出现解(sum(path)> target),return
  • 当遍历到决策树的叶子节点时(sum(path)==target)时,将当前结果的数组 path 放入答案数组 res中,递归停止。

3.遍历过程:

  • 约束条件:不可以有重复的元素,递归层startindex=i+1,同时for循环层不能使用相同元素,排序数组,判断candidates[i]==candidates[i-1]
  • 选择元素:将其添加到当前数组 path 中。
  • 递归搜索:在选择该元素的情况下,继续递归选择剩下元素。
  • 撤销选择:将该元素从当前结果数组 path 中移除。
class Solution:def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:res = []path = []candidates.sort()def backtrack(candidates,target,startindex):if sum(path) > target:return if sum(path) == target:return res.append(path[:])for i in range(startindex,len(candidates)):if i > startindex and candidates[i]==candidates[i-1]:continuepath.append(candidates[i])backtrack(candidates,target,i+1)path.pop()backtrack(candidates, target,0)return res

131. 分割回文串

思路:

1.确定回溯函数参数:定义全局遍历存放res集合和单个path,还需要

  • s字符

  • startindex(int)为下一层for循环搜索的起始位置。

2.终止条件:

  • startindex>=len(s),加入path

3.遍历过程:取temp = s[startindex:i+1],若temp为回文串,加入path,不是直接 跳过

注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1

class Solution:def partition(self, s: str) -> List[List[str]]:res = []path = []def  backtrack(s,startindex):if startindex >= len(s):return res.append(path[:])for i in range(startindex,len(s)):temp = s[startindex:i+1]if temp==temp[::-1]:path.append(temp)backtrack(s,i+1)path.pop()else:continuebacktrack(s,0)return res
http://www.lryc.cn/news/59217.html

相关文章:

  • 性能测试总结-根据工作经验总结还比较全面
  • 类型断言[as语法 | <> 语法
  • barret reduction原理详解及硬件优化
  • NLP / LLMs中的Temperature 是什么?
  • c#快速入门~在java基础上,知道C#和JAVA 的不同即可
  • nginx--基本配置
  • R语言中apply系列函数详解
  • 红黑树探险:从理论到实践,一站式掌握C++红黑树
  • CDH6.3.2大数据集群生产环境安装(七)之PHOENIX组件安装
  • 【C++要笑着学】搜索二叉树 (SBTree) | K 模型 | KV 模型
  • 微信小程序开发 | 小程序开发框架
  • 气候系统设计
  • 如何使用Thymeleaf给web项目中的网页渲染显示动态数据?
  • 01 | 电机常用语
  • Leetcode.2601 质数减法运算
  • DP7416国产192K数字音频接收芯片兼容替代CS8416
  • 全球土壤湿度数据获取方法
  • 在proteus中仿真arduino实现矩阵键盘程序
  • 【ROS2指南-5】理解ROS2服务
  • 探索Apache Hudi核心概念 (3) - Compaction
  • 100Wqps异地多活,得物是怎么架构的?
  • 35岁的测试工程师被公司强行辞退,感叹道:我以前就该好好努力了
  • ASP.NET动态Web开发技术第5章
  • 【数据结构与算法篇】时间复杂度与空间复杂度
  • HTTP API接口设计规范
  • 数据一致性校验(pt-table-checksum)
  • Talk预告 | 新加坡国立大学郑奘巍 AAAI‘23 杰出论文:大批量学习算法加速推荐系统训练
  • 肖 sir_就业课__004项目流程(H模型)
  • snipaste 截图工具——可以使图片悬浮在任何软件上,方便对比
  • Docker 快速部署Springboot项目