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

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

文章目录

    • 39.组合总和
    • 40.组合总和II
    • 131.分割回文串

39.组合总和

文字讲解:组合总和

视频讲解:组合总和

状态: 此题ok

思路:

代码:

class Solution {int sum;public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();backTracking(result, tempList, candidates, 0, target);return result;}public void backTracking(List<List<Integer>> result, LinkedList<Integer> tempList, int[] candidates, int startIndex, int target) {if (!tempList.isEmpty()&&sum>target) {return;}if (!tempList.isEmpty() && sum==target) { //收集List<Integer> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < candidates.length; i++) {tempList.offer(candidates[i]);sum+=candidates[i];backTracking(result, tempList, candidates, i, target);tempList.pollLast();sum-=candidates[i];}}
}

40.组合总和II

文字讲解:组合总和II

视频讲解:组合总和II

状态:这题一开始想先排序后去重,再做回溯;想岔批了,应该要保证前后处理相同元素的时候,要进行跳过来进行去重;

思路:

1、这一题,注意枝剪操作,即sum + candidates[i] <= target;不然提交leetcode会超时;

代码:

class Solution {int sum;public List<List<Integer>> combinationSum2(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();Arrays.sort(candidates);//完成去重backTracking(result, tempList, candidates, 0, target);return result;}public void backTracking(List<List<Integer>> result, LinkedList<Integer> tempList, int[] candidates, int startIndex, int target) {if (!tempList.isEmpty()&&sum==target) {List<Integer> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++) {if (i>startIndex && candidates[i]==candidates[i-1]) {continue;}tempList.offer(candidates[i]);sum+=candidates[i];backTracking(result, tempList, candidates, i+1, target);tempList.pollLast();sum-=candidates[i];}}
}

131.分割回文串

文字讲解:分割回文串

视频讲解:分割回文串

状态:这题细细一想,和前两题也是类似换汤不换药,看来还是对回溯-排列问题理解不够透彻;

思路:

代码:

class Solution {List<List<String>> result = new ArrayList<>();LinkedList<String> tempList = new LinkedList<>();public List<List<String>> partition(String s) {backTracking(s, 0);return result;}public void backTracking(String s, int startIndex) {if (startIndex>=s.length()) {List<String> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < s.length(); i++) {if(isPalindrome(s, startIndex, i)) {tempList.offer(s.substring(startIndex, i+1));} else {continue;}backTracking(s, i+1);tempList.pollLast();}}public boolean isPalindrome(String s, int start, int end) {while (start<=end) {if (s.charAt(start) == s.charAt(end)) {start++;end--;} else {return false;}}return true;}
}
http://www.lryc.cn/news/339324.html

相关文章:

  • 24年重庆三支一扶报名照不通过怎么处理?
  • 20240409在全志H3平台的Nano Pi NEO CORE开发板上运行Ubuntu Core16.04时跑通4G模块EC200A-CN【PPP模式】
  • 【示例】MySQL-不同case下索引的使用分析
  • MySQL表空间管理与优化(8/16)
  • 杂货铺 | Linux虚拟机Ubuntu操作系统下设置共享文件夹(以及找不到hgfs文件夹怎么办)
  • 《HF经理》:二认知误区
  • ELK日志分析系统之Zookeeper
  • 家居网购项目(Ajax验证用户名+上传图片)
  • 09 Php学习:超级全局变量
  • 【Java】SpringBoot快速整合mongoDB
  • UI设计的未来发展
  • 推荐系统学习记录——连续的嵌入空间
  • 【Entity Framework】你要知道EF中功能序列与值转换
  • 顶顶通呼叫中心中间件-SIP分机安全(mod_cti基于FreeSWITCH)
  • CountDownLatch
  • Vue3中的组合式API与选项式API:深入理解与比较
  • 接口自动化测试实战之接口概念、项目简介及测试流程问答!
  • 浏览器工作原理与实践--跨站脚本攻击(XSS):为什么Cookie中有HttpOnly属性
  • Ubuntu配置VScode的C++环境
  • 使用Code开发Django_模版和CSS
  • Llama 3下月正式发布,继续开源!
  • 有图片转成PDF文件格式的方法吗?分享图片转成PDF文件的方法
  • 数据结构---绪论
  • matlab 安装 mingw64(6.3.0),OPENEXR
  • 最新彩虹知识付费商城源码 V3.4
  • Redis实现延迟任务的几种方案
  • 一种springboot请求参数校验的实现方案
  • 盒子模型+响应式布局 + 原型链与继承
  • 面试准备 集合 List
  • Java快速入门系列-7(测试与调试)