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

Day23| 39. 组合总和、40.组合总和II、131.分割回文串

39. 组合总和

与前面几题的区别是:

  1. 这道题中元素可以重复

  2. 用组合限制树的深度

回溯三部曲:

  1. 递归函数参数 集合candidates、目标值target、和sum、控制for循环的起始位置startIndex

  2. 递归终止条件 终止的·两个条件:sum大于target;sum等于target

  3. 单层搜索的逻辑 单层for循环依然是从startIndex开始,搜索candidates集合。

public class Solution {public IList<IList<int>> res=new List<IList<int>>();public IList<int> path=new List<int>();public IList<IList<int>> CombinationSum(int[] candidates, int target) {BackTracking(candidates,target,0,0);return res;}public void BackTracking(int[] candidates, int target,int sum,int startIndex){if(sum>target) return;if(sum==target){res.Add(new List<int>(path));return;}for(int i=startIndex;i<candidates.Length;i++){sum+=candidates[i];path.Add(candidates[i]);BackTracking(candidates,target,sum,i);sum-=candidates[i];path.RemoveAt(path.Count-1);}}
}

40.组合总和II

这道题目和39.组合总和 (opens new window)如下区别:

  1. 本题candidates 中的每个数字在每个组合中只能使用一次。

  2. 本题数组candidates的元素是有重复的,而39.组合总和 (opens new window)是无重复元素的数组candidates

去重:

  • 树枝去重:一个维度是同一树枝上使用过

  • 树层去重:一个维度是同一树层上使用过(采用)

步骤:

  • 排序输入数组 candidates

  • 便于剪枝

  • 便于发现重复数字并进行去重处理

  • 使用回溯法进行组合搜索

  • 每次递归遍历后续的数字(i+1),避免重复使用

  • 路径合法(和为 target)就加入结果

  • 去重关键:跳过重复元素

  • 在同一层 for 循环中,如果当前数字和上一个相同,跳过它

public class Solution {public IList<IList<int>> res = new List<IList<int>>();  // 存放最终结果集合public IList<int> path = new List<int>();               // 临时路径(当前组合)public IList<IList<int>> CombinationSum2(int[] candidates, int target) {Array.Sort(candidates); // 先排序,方便剪枝和去重Backtrack(candidates, target, 0, 0);return res;}public void Backtrack(int[] candidates, int target, int start, int sum) {if (sum == target) {res.Add(new List<int>(path)); // 如果和等于目标,加入结果return;}for (int i = start; i < candidates.Length; i++) {int cur = candidates[i];// 剪枝:当前数 + 现有和已经超过 target,后面更大直接跳过if (sum + cur > target) break;// 去重:同层中遇到相同元素,跳过(i > start 表示是“同一层”)if (i > start && candidates[i] == candidates[i - 1]) continue;// 做选择path.Add(cur);Backtrack(candidates, target, i + 1, sum + cur); // i + 1 表示不能重复选同一个元素path.RemoveAt(path.Count - 1); // 撤销选择(回溯)}}
}

131.分割回文串

回文字符串(Palindromic String)是指从左到右读和从右到左读完全相同的字符串,其字符序列呈现 “对称” 特征

  • 单个字符:"a""5""#"(单个字符本身就是回文)。

  • 两个字符:"aa""22""##"(两个相同字符构成回文)。

  • 多个字符:

  • 英文:"madam"( Madam 倒读仍是 Madam)、"level"( Level 倒读仍是 Level)。

  • 中文:"上海自来水来自海上""黄山落叶松叶落山黄"

  • 数字 / 符号:"12321""#@#"

🚀 核心算法:回溯 + 剪枝(判断是否是回文)

我们从左往右依次枚举字符串的切分点:

  • 每次切一段子串 s[start..i]

  • 如果是回文,就加入当前路径 path

  • 然后递归处理剩下的字符串 s[i+1..]

  • 直到走到字符串末尾,说明找到了一种可行方案,加入结果集

public class Solution{public IList<IList<string>> res=new List<IList<string>>();public IList<string> path=new List<string>();public IList<IList<string>> Partition(string s) {Backtracking(s,0);return res;}public void Backtracking(string s,int start){if(start==s.Length){res.Add(new List<string>(path));return;}for(int i=start;i<s.Length;i++){if(IsPalindrome(s,start,i)){path.Add(s.Substring(start,i-start+1)); // 加入路径Backtracking(s,i+1);path.RemoveAt(path.Count-1);}}}// 判断字符串s从left到right是否是回文private bool IsPalindrome(string s,int left,int right){while(left<right){if(s[left]!=s[right]) return false;left++;right--;}return true;}
}

http://www.lryc.cn/news/591389.html

相关文章:

  • 【47】MFC入门到精通——MFC编辑框 按回车键 程序闪退问题 ,关闭 ESC程序退出 问题
  • 泛型与类型安全深度解析及响应式API实战
  • python网络爬虫(第二步:安装浏览器驱动,驱动浏览器加载网页、批量下载资源)
  • 板凳-------Mysql cookbook学习 (十一--------12)
  • 20250717在荣品的PRO-RK3566开发板的Android13系统下解决点屏出现问题unsupport command data type: 217
  • x3CTF-2025-web-复现
  • 深度学习 -- Tensor属性及torch梯度计算
  • 计算机的网络体系及协议模型介绍
  • 外贸ERP软件有哪些?八大热门erp软件功能测评
  • centos中新增硬盘挂载文件夹
  • 河南萌新联赛2025第(一)场:河南工业大学(补题)
  • 亚远景科技助力长城汽车,开启智能研发新征程
  • 视频安全新思路:VRM视频分片错序加密技术
  • C++性能优化与现代工程实践:打造高效可靠的软件系统
  • C++性能优化
  • 91套商业策划创业融资计划书PPT模版
  • Java Stream API性能优化:原理深度解析与实战指南
  • PyTorch边界感知上下文神经网络BA-Net在医学图像分割中的应用
  • 多端协同的招聘系统源码开发指南:小程序+APP一体化设计
  • Android 实现:当后台数据限制开启时,仅限制互联网APN。
  • 小程序按住说话
  • 紫金桥跨平台监控组态软件 | 功能强大,支持复杂工业场景,与西门子 PLC 无缝兼容
  • 【Linux基础知识系列】第五十二篇 - 初识Linux的内置命令
  • 三十四、【扩展工具篇】JSON 格式化与解析:集成 Monaco Editor 打造在线 JSON 工具
  • 物联网主机在化工园区安全风险智能化管控平台中的应用
  • day055-Dockerfile与常用指令
  • PyCharm 高效入门指南(引言 + 核心模块详解)
  • 【C# in .NET】16. 探秘类成员-索引器:通过索引访问对象
  • 关于接口测试的HTTP基础【接口测试】
  • 解读一个大学专业——信号与图像处理