代码随想录算法训练营day26|39. 组合总和、40. 组合总和||、8.分割回文串

2024-06-21 05:28

本文主要是介绍代码随想录算法训练营day26|39. 组合总和、40. 组合总和||、8.分割回文串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

39. 组合总和

由题意可知,数组中的每一个数都可以重复相加,因此我们在绘制树形图的时候,每次取完某一个数,下一次回溯的时候还可以用该数,比如2、3、6,每次取完2,候选还剩2、3、6。而最后答案也确实是2、2,所以2每次取完不能排除。
另外对于存储满足条件结果的path,每次从纵向的回溯过程出来之后,要将这一层回溯加进去的值减掉给横向的循环的下一个值腾出空间,一边进入下一个值的回溯。同时要给一个sum的值记录和。
在这里插入图片描述

class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();ArrayList<Integer> path = new ArrayList<Integer>();public List<List<Integer>> combinationSum(int[] candidates, int target) {if(candidates.length == 0 || candidates == null) return res;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 ArrayList<Integer>(path));//循环for(int i = startIndex;i < candidates.length;i++){sum += candidates[i];path.add(candidates[i]);backTracking(candidates,target,sum,i);sum -= candidates[i];path.remove(path.size()-1);}}
}

40. 组合总和||

我觉得这道题目卡个2讲的已经够好了,多看几遍理解透彻就行。主要就是需要对candidates进行排序,然后理解去重的条件为什么是:

      if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {continue;}

最后的代码是:
其中:used是用来记录该位置的candidates的值是否被用过

class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();ArrayList<Integer> path = new ArrayList<Integer>();int sum = 0;boolean[] used;public List<List<Integer>> combinationSum2(int[] candidates, int target) {used = new boolean[candidates.length];Arrays.fill(used, false);Arrays.sort(candidates);backTracking(candidates,target,0);return res;}public void backTracking(int[] candidates, int target,int startIndex){if(sum == target) res.add(new ArrayList<Integer>(path));for(int i = startIndex;i < candidates.length;i++){if(sum + candidates[i] > target){break;}// 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {continue;}sum += candidates[i];used[i] = true;path.add(candidates[i]);backTracking(candidates,target,i+1);sum -= candidates[i];used[i] = false;path.remove(path.size()-1);}}
}

8.分割回文串

class Solution {List<List<String>> res = new ArrayList<List<String>>();ArrayList<String> path = new ArrayList<String>();public List<List<String>> partition(String s) {backTracking(s,0);return res;}public void backTracking(String s,int startIndex){//如果起始值到达字符串末尾,结束递归if(startIndex >= s.length()){res.add(new ArrayList<>(path));return;}for (int i = startIndex; i < s.length(); i++) {if(judgeHuiWen(s,startIndex,i)){path.add(s.substring(startIndex,i+1));}else{continue;//有一个子字符串不是回文字符串就没有必要进行回溯了}//如果现层的子字符串为回文的,继续回溯backTracking(s,i+1);//i往下一位,继续回溯//回溯path.remove(path.size()-1);}}public boolean judgeHuiWen(String s,int startIndex,int endIndex){while(startIndex <= endIndex){if(s.charAt(startIndex) != s.charAt(endIndex)) return false;startIndex++;endIndex--;}return true;}
}

这篇关于代码随想录算法训练营day26|39. 组合总和、40. 组合总和||、8.分割回文串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1080324

相关文章

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python如何将大TXT文件分割成4KB小文件

《Python如何将大TXT文件分割成4KB小文件》处理大文本文件是程序员经常遇到的挑战,特别是当我们需要把一个几百MB甚至几个GB的TXT文件分割成小块时,下面我们来聊聊如何用Python自动完成这... 目录为什么需要分割TXT文件基础版:按行分割进阶版:精确控制文件大小完美解决方案:支持UTF-8编码

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La