代码学习记录22--回溯算法第三天

随想录日记part22

t i m e : time: time 2024.03.17



主要内容:今天主要是结合类型的题目加深对回溯算法的理解:1.组合总和;2.组合总和
;3.分割回文串。

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


Topic1组合总和

题目:

给你一个无重复元素的整数数组 c a n d i d a t e s candidates candidates 和一个目标整数 t a r g e t target target ,找出 c a n d i d a t e s candidates candidates 中可以使数字和为目标数 t a r g e t target target 的所有不同组合 ,并以列表形式返回。你可以按任意顺序返回这些组合。
c a n d i d a t e s candidates candidates 中的同一个数字可以无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 t a r g e t target target 的不同组合数少于 150 个。

输入: c a n d i d a t e s = [ 2 , 3 , 6 , 7 ] , t a r g e t = 7 candidates = [2,3,6,7], target = 7 candidates=[2,3,6,7],target=7
输出: [ [ 2 , 2 , 3 ] , [ 7 ] ] [[2,2,3],[7]] [[2,2,3],[7]]

思路: 按照回溯模板我们进行回溯三部曲:
递归三部曲:

1.回溯函数模板返回值以及参数
在这里要定义两个全局变量, p a t h path path用来存放符合条件单一结果, r e s u l t result result用来存放符合条件结果的集合。回溯函数里一定有一个参数记录当前 p a t h path path里面值的和 n o w s u m nowsum nowsum;还需要一个参数为 i n t int int 型变量 s t a r t I n d e x startIndex startIndex
所以整体代码如下:

List<List<Integer>> result=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
void backtracking(int target, int start, int nowsum, int[] candidates)

2.回溯函数终止条件
回溯出口,如果 t a r g e t target target 里面的数量等于 n o w s u m nowsum nowsum,说明其到达叶子节点则将其加入到 r e s u l t result result,否则直接返回 r e t u r n return return
代码如下:

if (nowsum > target)
            return;
        if (target == nowsum) {
            result.add(new ArrayList<>(path));
            return;
        }

3.回溯搜索的遍历过程
f o r for for 循环每次从 s t a r t I n d e x startIndex startIndex 开始遍历,然后用 p a t h path path 保存取到的节点i搜索的过程如下图:
在这里插入图片描述

实现代码如下:

for (int i = start; i < candidates.length; i++) {
            path.add(candidates[i]);
            nowsum += candidates[i];
            backtracking(target, i, nowsum, candidates);// 不用i+1了,表示可以重复读取当前的数
            nowsum -= candidates[i];
            path.removeLast();
        }

完整的代码如下:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new LinkedList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        result.clear();
        path.clear();
        backtracking(target, 0, 0, candidates);
        return result;
    }

    private void backtracking(int target, int start, int nowsum, int[] candidates) {
        if (nowsum > target)
            return;
        if (target == nowsum) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            path.add(candidates[i]);
            nowsum += candidates[i];
            backtracking(target, i, nowsum, candidates);// 不用i+1了,表示可以重复读取当前的数
            nowsum -= candidates[i];
            path.removeLast();
        }
    }
}


Topic2组合总和||

题目:

给定一个候选人编号的集合 c a n d i d a t e s candidates candidates 和一个目标数 t a r g e t target target ,找出 c a n d i d a t e s candidates candidates 中所有可以使数字和为 t a r g e t target target 的组合。 c a n d i d a t e s candidates candidates 中的每个数字在每个组合中只能使用 一次 。注意:解集不能包含重复的组合。

输入: c a n d i d a t e s = [ 10 , 1 , 2 , 7 , 6 , 1 , 5 ] , t a r g e t = 8 candidates = [10,1,2,7,6,1,5], target = 8 candidates=[10,1,2,7,6,1,5],target=8
输出: [ [ 1 , 1 , 6 ] , [ 1 , 2 , 5 ] , [ 1 , 7 ] , [ 2 , 6 ] ] [ [1,1,6], [1,2,5], [1,7], [2,6] ] [[1,1,6],[1,2,5],[1,7],[2,6]]

思路: 按照回溯模板我们进行回溯三部曲:
递归三部曲:

1.回溯函数模板返回值以及参数
在这里要定义两个全局变量, p a t h path path用来存放符合条件单一结果, r e s u l t result result用来存放符合条件结果的集合。回溯函数里一定有一个参数记录当前 p a t h path path里面值的和 n o w s u m nowsum nowsum;还需要一个参数为 i n t int int 型变量 s t a r t I n d e x startIndex startIndex,还有一个用于记录是否被使用过的数组 u s e d used used
所以整体代码如下:

List<List<Integer>> result=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
void reback(int[] candidates,int target,int nowsum,int startindex)
boolean[] used;//记录元素是否被用过

2.回溯函数终止条件
回溯出口,如果索引值 s t a r t i n d e x startindex startindex 里面的数量等于 d i g i t s . l e n g t h ( ) digits.length() digits.length(),说明其到达叶子节点,则将 t e m p temp temp其加入到 l i s t list list,否则直接返回 r e t u r n return return
代码如下:

if(target<nowsum)return;
        if(target==nowsum){
            result.add(new ArrayList<>(path));
        }

3.回溯搜索的遍历过程
首先将原始数据进行排序,进行排序后相同的数字就会相邻。如果 c a n d i d a t e s [ i ] = = c a n d i d a t e s [ i − 1 ] 并且 u s e d [ i − 1 ] = = f a l s e candidates[i] == candidates[i - 1] 并且 used[i - 1] == false candidates[i]==candidates[i1]并且used[i1]==false,就说明:前一个树枝,使用了 c a n d i d a t e s [ i − 1 ] candidates[i - 1] candidates[i1],也就是说同一树层使用过 c a n d i d a t e s [ i − 1 ] candidates[i - 1] candidates[i1]。此时 f o r for for 循环里就应该做 c o n t i n u e continue continue 的操作。
在这里插入图片描述

实现代码如下:

for(int i=startindex;i<candidates.length;i++){
            if(i>startindex && candidates[i]==candidates[i-1])continue;
            nowsum+=candidates[i];
            path.add(candidates[i]);
            used[i]=true;
            reback(candidates,target,nowsum,i+1);
            nowsum-=candidates[i];
            path.removeLast();
            used[i]=false;
        }

完整的代码如下:

class Solution {
    List<List<Integer>> result=new ArrayList<>();//用于记录最后的结果
    List<Integer> path=new LinkedList<>();//用于记录临时结果
    boolean[] used;//记录元素是否被用过
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        result.clear();
        path.clear();
        used=new boolean[candidates.length];
        Arrays.fill(used, false);
        reback(candidates,target,0,0);
        return result;
    }
    private void reback(int[] candidates,int target,int nowsum,int startindex){
        if(target<nowsum)return;
        if(target==nowsum){
            result.add(new ArrayList<>(path));
        }
        for(int i=startindex;i<candidates.length;i++){
            if(i>startindex && candidates[i]==candidates[i-1])continue;
            nowsum+=candidates[i];
            path.add(candidates[i]);
            used[i]=true;
            reback(candidates,target,nowsum,i+1);
            nowsum-=candidates[i];
            path.removeLast();
            used[i]=false;
        }
    }
    private void reback1(int[] candidates,int target,int nowsum,int startindex){
        if(target<nowsum)return;
        if(target==nowsum){
            result.add(new ArrayList<>(path));
        }
        for(int i=startindex;i<candidates.length;i++){
            if(i>startindex && candidates[i]==candidates[i-1]){
                continue;
            }
            nowsum+=candidates[i];
            path.add(candidates[i]);
            reback(candidates,target,nowsum,i+1);
            nowsum-=candidates[i];
            path.removeLast();
        }
    }
}

时间复杂度: O ( n ∗ 2 n ) O(n * 2^n) O(n2n)
空间复杂度: O ( n ) O(n) O(n)



Topic3分割回文串

题目:

给你一个字符串 s s s,请你将 s s s 分割成一些子串,使每个子串都是回文串。返回 s s s 所有可能的分割方案。

输入: s = " a a b " s = "aab" s="aab"
输出: [ [ " a " , " a " , " b " ] , [ " a a " , " b " ] ] [["a","a","b"],["aa","b"]] [["a","a","b"],["aa","b"]]

思路:

解决该问题需要解决下面几个问题:

  • 切割问题可以抽象为组合问题
  • 如何模拟那些切割线
  • 切割问题中递归如何终止
  • 在递归循环中如何截取子串

如何判断回文按照回溯模板我们进行回溯三部曲:
递归三部曲:

1.回溯函数模板返回值以及参数
在这里要定义两个全局变量, p a t h path path用来存放符合条件单一结果, r e s u l t result result用来存放符合条件结果的集合。回溯函数里一定有一个参数记录当前 p a t h path path里面值;还需要一个参数为 i n t int int 型变量 i n d e x index index
所以整体代码如下:

List<List<String>> result = new ArrayList<>();// 存最后的结果
Deque<String> path = new LinkedList<>();// 存中间的结果
void reback(String s, int index)

2.回溯函数终止条件
回溯出口,从树形结构的图中可以看出:切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止条件。
在这里插入图片描述

代码如下:

 if (index >= s.length()) {
            result.add(new ArrayList(path));
            return;
        }

3.回溯搜索的遍历过程
首先判断这个子串是不是回文,如果是回文,就加入在 p a t h path path中, p a t h path path 用来记录切割过的回文子串。

实现代码如下:

for (int i = index; i < s.length(); i++) {
            if (isHuiwen(s, index, i)) {
                String str = s.substring(index, i + 1);
                path.addLast(str);
            } else {
                continue;
            }
            reback(s, i + 1);
            path.removeLast();
        }

完整的代码如下:

class Solution {
    List<List<String>> result = new ArrayList<>();// 存最后的结果
    Deque<String> path = new LinkedList<>();// 存中间的结果

    public List<List<String>> partition(String s) {
        reback(s, 0);
        return result;
    }

    private void reback(String s, int index) {
        if (index >= s.length()) {
            result.add(new ArrayList(path));
            return;
        }
        for (int i = index; i < s.length(); i++) {
            if (isHuiwen(s, index, i)) {
                String str = s.substring(index, i + 1);
                path.addLast(str);
            } else {
                continue;
            }
            reback(s, i + 1);
            path.removeLast();
        }
        
    }
    private boolean isHuiwen(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/469794.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Leetcode 79. 单词搜索

心路历程&#xff1a; 做完这道题才发现是回溯&#xff0c;一开始想的是递归&#xff0c;判断完第i个字符后&#xff0c;只需要挨个判断第i1个字符在不在第i个字符的邻域。后来发现由于不能重复使用元素&#xff0c;所以需要维护一个visited列表&#xff0c;并且在遍历所有可能…

嵌入式3-19

1、哈希表的代码写完&#xff0c;写出给出关键字&#xff0c;找到该关键字在哈希表(指针数组)中下标的位置&#xff0c;以及在链表中的位置。(因为返回值只有一个&#xff0c;所以结果直接找到通过输出语句输出) void search(node *H,int key); 2、快速排序和折半查找的代码写…

Maven项目如何导入依赖包

一、导入依赖包 导入mysql依赖包 第一步&#xff1a;登录Maven官网 Maven官网&#xff1a;https://mvnrepository.com/search?qmysql 第二步&#xff1a;点击MySql Connector Java 第三步&#xff1a;点击任意一个版本 第四步&#xff1a;将以下内容复制到pom.xml中 导入j…

Unity发布webgl设置占满浏览器运行

Unity发布webgl设置占满浏览器运行 Unity发布webgl的时候index.html的模板文件 模板文件路径&#xff0c;根据自己的需求修改。 C:\Program Files\Unity\Hub\Editor\2021.1.18f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Default再桌面新建一个t…

随笔-生老病死

周末两天也没有出门&#xff0c;帮着一个朋友做了些图&#xff08;就这两天忙不过来&#xff09;&#xff0c;挣了点外快&#xff08;700&#xff09;&#xff0c;累得腰酸、眼花、脖子疼。 媳妇带着小孩出去玩&#xff0c;中间发了个视频&#xff0c;是小孩进了一个围棋培训班…

HTML基础:列表标签的3种形式以及嵌套列表

你好&#xff0c;我是云桃桃。 一个希望帮助更多朋友快速入门 WEB 前端程序媛。 g.zh后台回复“前端工具”可免费获取开发工具&#xff0c;持续更新 今天聊聊列表标签。列表标签&#xff0c;在网页设计中可以帮助将信息以结构化的方式呈现给用户&#xff0c;提高信息的可读性…

在线教育平台帮助教培机构打造线上

随着互联网的迅猛发展&#xff0c;在线教育逐渐成为了教育行业的主流趋势。为了满足教育机构和学生对高效、便捷在线教育的需求&#xff0c;乔拓云教育系统应运而生。该系统以学员端展示课程和后台管理教务为核心功能&#xff0c;为教育机构提供了一站式解决方案&#xff0c;助…

母亲的奶牛(bfs)

农夫约翰有三个容量分别为 A , B , C A,B,C A,B,C 升的挤奶桶。 最开始桶 A A A 和桶 B B B 都是空的&#xff0c;而桶 C C C 里装满了牛奶。 有时&#xff0c;约翰会将牛奶从一个桶倒到另一个桶中&#xff0c;直到被倒入牛奶的桶满了或者倒出牛奶的桶空了为止。 这一过…

晶圆制造过程中常用载具的类型

晶圆载具用于硅片生产、晶圆制造以及工厂之间晶圆的储存、传送、运输以及防护。晶圆载具种类很多,如FOUP用于晶圆制造工厂中晶圆的传送;FOSB用于硅片生产与晶圆制造工厂之间的运输;CASSETTE载具可用于工序间运送以及配合工艺使用。 OPEN CASSETTE OPEN CASSETTE主要在晶圆…

实战http请求

文章目录 使用python3的标准库发起GET请求使用python3的标准库发起POST请求使用requests库发起GET请求使用requests库发起POST请求使用java 11内置的http client发起访问百度请求使用java 11内置的http client发起访问POST请求进一步阅读与参考资料 使用python3的标准库发起GET…

3.19学习总结

一.题解分析 这是一道bfs的题目,初看毫无头绪,经过点拨后恍然大悟,我们需要记录其六个操作,对每次选择时每个操作进行入队检查,最终得到任意一个罐子里的水等于c,记录其总操作步数,并进行输出.这里的操作类似于走迷宫时,我们设置的方向数组.然后我们在输出操作时,可以用一个数组…

1-postgresql数据库高可用脚本详解

问题&#xff1a; pgrep -f postgres > /dev/null && echo 0 || pkill keepalived 这是什么意思 建议换成 pgrep -f postmaster > /dev/null && echo 0 || pkill keepalived 回答 这条命令是一个复合命令&#xff0c;包含条件执行和重定向的元素。让我们…

Springboot+Redis:实现缓存 减少对数据库的压力

&#x1f389;&#x1f389;欢迎光临&#xff0c;终于等到你啦&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;持续更新的专栏Redis实战与进阶 本专栏讲解Redis从原理到实践 …

根据自己的想法去模拟实现库函数(1)

由于最近比较忙&#xff0c;导致好久没更新了。想我没&#xff1f;嘻嘻&#xff0c;不闹了&#xff0c;开始我们今天的小课堂吧&#xff01; 什么&#xff0c;你想上课走神&#xff1f;小心二叔给你梳头哦&#xff01; 那么这篇文章就先带大家去模拟一下strlen这个函数吧。 st…

01 JDBC介绍

文章目录 JDBC本质版本使用核心APIDriverDriverManager驱动注册连接对象获取 Connection获取执行对象事务管理 Statement概述 ResultSet概述 JDBC本质 官方&#xff08;sun公司&#xff09;定义的一套操作所有关系型数据库的规则&#xff0c;即接口各个数据库厂商去实现这套接…

利用Python爬虫获取xx数据

目录 一、前言 二、requests 请求库 1、requests 安装 2、requests 的基本使用 三、Beautiful Soup 1、Beautiful Soup 安装 2、BeautifulSoup对象介绍与创建 3、BeautifulSoup对象的find方法 四、总结 一、前言 什么是爬虫&#xff1f; 网络爬虫&#xff08;又被称为…

外键约束

目录 外键约束 对数据表进行初期设计&#xff0c;暂时不使用外键 验证限制三 验证级联删除 设置级联更新 Oracle从入门到总裁:​​​​​​https://blog.csdn.net/weixin_67859959/article/details/135209645 外键约束 外键约束主要是在父子表关系中体现的一种约束操作。…

【C++】string 类---字符判断与大小写转换(超详细解析!)

目录 一、string 类的介绍 二、字符大小写转换与判断常用函数 &#x1f4a6; 字符大小写判断 ① isalpha() ② isalnum() ③ isdigit() ④ islower() ⑤ isupper() &#x1f4a6; 字符大小写转换 ① tolower() ✨方法一&#xff1a; ✨方法二&#xff1a; ② toupper() ✨方…

实现:mysql-5.7.42 到 mysql-8.2.0 的升级(二进制方式)

实现&#xff1a;mysql-5.7.42 到 mysql-8.2.0 的升级&#xff08;二进制方式&#xff09; 1、操作环境1、查看当前数据库版本2、操作系统版本3、查看 Linux 系统上的 glibc&#xff08;GNU C 库&#xff09;版本&#xff08;**这里很重要&#xff0c;要下载对应的内核mysql版本…

Java之全体集合!

介绍 容器&#xff0c;就是可以容纳其他Java对象的对象。Java Collections Framework(JCF)为Java开发者提供了通用的容器&#xff0c;其始于JDK 1.2.优点是: 降低编程难度提高程序性能提高API间的互操作性降低学习难度降低设计和实现相关API的难度增加程序的重用性 Java容器里…
最新文章