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

免费的写作网站鄂州建设网站

免费的写作网站,鄂州建设网站,设计官网和推广的公司,沧州网站建设培训学校39.组合总和 思路: 1.确定回溯函数参数:定义全局遍历存放res集合和单个path,还需要 candidates数组 targetSum(int)目标和。 startIndex(int)为下一层for循环搜索的起始位置。 2.终止条件…

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.yayakq.cn/news/168655/

相关文章:

  • 南京网站建设物业网站模板下载
  • 深圳做网站的公司哪家好未来产品设计
  • 网页设计实训报告大专北京seo编辑
  • 内蒙古网站开发网站备案域名证书
  • 刚开始做网站要传数据库吗做电音的软件的专业下载网站
  • 南京建设机械网站织梦自定义表单做网站在线留言
  • 外贸企业网站优化做外贸推广的公司
  • 新手学做网站12天婴域名做网站出售合法吗
  • 浙江省建设建材工会网站世界500强企业排名2024最新名单
  • 政务公开与网站建设工作总结存在问题和困难广东做网站策划
  • 做一个网站维护多少钱软件推广平台有哪些?哪个比较好
  • 平顶山住房和城乡建设厅网站龙岗网站建设联系电话
  • 网站not found黄骅烈士
  • 网站备案完了怎么做免费的外链平台
  • 做一个小说网站需要多少钱个人可以做淘宝客网站吗
  • 织梦高端大气网站模板深圳网站开发公
  • 手机网站欣赏网站制作工具 织梦
  • 商务网站大全wordpress获取微博内容
  • 社交网站 备案哪个网站做美食视频网站
  • 用ps做租房网站里的图标大小seo社区
  • 网站开发规范有哪些wordpress5.1用什么php版本
  • 网站后台管理系统管理员登录广州做网站一般要多少钱
  • 壹六八信息科技网站建设教育机构如何引流与推广
  • 上海网站建设联系电从零开始学做视频剪辑
  • 山东济南网站建设做网站图片
  • 海洋公司做网站深圳建筑工程交易服务中心网
  • 网站建设的技术体会全国网站开发
  • dedecms 做微网站装潢设计软件有哪些
  • 五大搜索引擎 三大门户网站深圳网站建设 外包合作
  • 安徽网站设计费用网站建设费属于广告费