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

23th Day| 39.组合总和,40.组合总和II,131.分割回文串

LeetCode 39 组合总和

题目链接:39.组合总和

//剪枝优化
class Solution {List<Integer> path = new ArrayList<>();List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);backtracking(candidates, target,0, 0);return res;}public void backtracking(int[] nums, int target, int sum, int startIndex){if(sum == target){res.add(new ArrayList(path));return;}for(int i = startIndex; i < nums.length; i++){if(sum+nums[i] > target) break;path.add(nums[i]);backtracking(nums, target,sum+nums[i], i);//取得是当前元素path.removeLast();}}
}

LeetCode 40 组合总和II

题目链接:40.组合总和II

class Solution {List<Integer> path = new ArrayList<>();List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);backtracking(candidates, target, 0, 0);return res;}public void backtracking(int[] nums, int target, int sum, int startIndex){if(sum == target){res.add(new ArrayList(path));return;}for(int i = startIndex; i < nums.length; i++){if(sum+nums[i] > target) break;if(i != startIndex && nums[i-1] == nums[i]) continue;//i > startIndexpath.add(nums[i]);backtracking(nums, target, sum+nums[i], i+1);path.removeLast();}}
}

LeetCode 131 分割回文串

题目链接:131.分割回文串

class Solution {LinkedList<String> path = new LinkedList<>();List<List<String>> res = new ArrayList<>();public List<List<String>> partition(String s) {backtracking(s, 0 , new StringBuilder());return res;}//每一个for都需要一个新的StringBuilder;public void backtracking(String s, int startIndex, StringBuilder sb){if(startIndex == s.length()){res.add(new ArrayList(path));return;}for(int i = startIndex; i < s.length(); i++){sb.append(s.charAt(i));if(check(sb)){path.add(sb.toString());backtracking(s, i+1, new StringBuilder());path.removeLast();}else{continue;}}}public boolean check(StringBuilder sb){int i = 0;int j = sb.length() -1;while(i < j){if(sb.charAt(i++) != sb.charAt(j--)) return false;}return true;}
}

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

相关文章:

  • 数据结构---概念、数据与数据之间的关系(逻辑结构、物理结构)、基本功能、数据结构内容、单向链表(该奶奶、对象、应用)
  • 模型 古德哈特定律(Goodhart’s law)
  • 跨语言AI服务指标收集实战
  • 【深度学习】【三维重建】windows11环境配置PyTorch3d详细教程
  • 智能图书馆管理系统开发实战系列(五):前后端集成 - koffi调用与接口设计
  • WAIC引爆AI,智元机器人收购上纬新材,Geek+上市,157起融资撑起热度|2025年7月人工智能投融资观察 · 极新月报
  • FreeRTOS源码分析一:task启动(RISCV架构)
  • 【图像处理基石】用Python实现基础滤镜效果
  • PCB铜浆塞孔工艺流程
  • 网页操作自动化解决方案:如何用Browser-Use+CPolar提升企业运营效率
  • openwrt下安装istore(基于pve)
  • CCF IVC 2025“汽车安全攻防赛” -- Crypto -- WriteUp
  • ESP2025年6月认证C++八级( 第三部分编程题(2)遍历计数)
  • 线程池的实现
  • 【python】转移本地安装的python包
  • 【语音技术】意图与语料
  • 从下单到发货:如何清晰表达发货时间
  • Python编程基础与实践:Python条件语句入门:掌握if, else, 和elif
  • Android动画实现控件形状、大小逐渐过渡
  • Agentic RAG:自主检索增强生成的范式演进与技术突破
  • Waterfox水狐浏览器、火狐浏览器外观修改
  • XGBoost三部曲:XGBoost参数详解
  • Store / Slice / Reducer
  • 利用DeepSeek将Rust程序的缓冲输出改写为C语言实现提高输出效率
  • Python爬虫实战:研究SimpleCV技术,构建图像获取及处理系统
  • vulnhub-ELECTRICAL靶场攻略
  • 基于OAuth2与JWT的微服务API安全实战经验分享
  • AbstractExecutorService:Java并发核心模板解析
  • Batch Normalization(BN):深度学习中的“训练加速器”与实践指南
  • Vue 详情模块 3