代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串

news2024/9/26 1:26:41

一、参考资料

组合总和

题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html

视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ

组合总和II

题目链接/文章讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html

视频讲解:https://www.bilibili.com/video/BV12V4y1V73A

分割回文串

题目链接/文章讲解:https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html

视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6

二、LeetCode39. 组合总和

https://leetcode.cn/problems/combination-sum/description/

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

示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []

提示:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
candidates 的所有元素 互不相同
1 <= target <= 40

本题搜索的过程抽象成树形结构如下:

注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回!

而在77.组合 (opens new window)216.组合总和III (opens new window)中都可以知道要递归K层,因为要取k个元素的组合。

我也能自己写出代码啦,模板真香

class Solution {
private:
    vector<int> path;
    vector<vector<int>> res;

    void backtracking(vector<int> candidates, int target, int sum, int startIndex) {
        if (sum > target) return ;
        if (sum == target) {
            res.push_back(path);
            return ;
        }
        for (int i = startIndex; i < candidates.size(); i++) {
            sum += candidates[i];
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum, i);  // 不用i+1了,表示可以重复读取当前的数
            path.pop_back();
            sum -= candidates[i];
        }
    }
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        backtracking(candidates, target, 0, 0);
        return res;
    }
};

剪枝优化(这里没有详细研究,大体思路还是在循环的地方提前终止):

对总集合排序之后,如果下一层的sum(就是本层的 sum + candidates[i])已经大于target,就可以结束本轮for循环的遍历

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            result.push_back(path);
            return;
        }

        // 如果 sum + candidates[i] > target 就终止遍历
        for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
            sum += candidates[i];
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum, i);
            sum -= candidates[i];
            path.pop_back();

        }
    }
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        result.clear();
        path.clear();
        sort(candidates.begin(), candidates.end()); // 需要排序
        backtracking(candidates, target, 0, 0);
        return result;
    }
};

在求和问题中,排序之后加剪枝是常见的套路!

三、LeetCode40.组合总和II

https://leetcode.cn/problems/combination-sum-ii/description/

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

示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5, 输出: [ [1,2,2], [5] ]

提示:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30

先排序!再树层去重

  • used[i - 1] == true,说明同一树枝candidates[i - 1]使用过

  • used[i - 1] == false,说明同一树层candidates[i - 1]使用过

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
        if (sum == target) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
            // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
            // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
            // 要对同一树层使用过的元素进行跳过
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
                continue;
            }
            sum += candidates[i];
            path.push_back(candidates[i]);
            used[i] = true;
            backtracking(candidates, target, sum, i + 1, used); // 和39.组合总和的区别1,这里是i+1,每个数字在每个组合中只能使用一次
            used[i] = false;
            sum -= candidates[i];
            path.pop_back();
        }
    }

public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<bool> used(candidates.size(), false);
        path.clear();
        result.clear();
        // 首先把给candidates排序,让其相同的元素都挨在一起。
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0, used);
        return result;
    }
};

我写的如下~

class Solution {
private:
    vector<int> path;
    vector<vector<int>> res;
    void backtracking(vector<int> candidates, int target, int sum, int startIndex, vector<bool> used) {
        if (sum > target) return ;
        if (sum == target) {
            res.push_back(path);
            return ;
        }
        for (int i = startIndex; i < candidates.size(); i++) {
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;
            sum += candidates[i];
            used[i] = true;
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum, i + 1, used);
            path.pop_back();
            used[i] = false;
            sum -= candidates[i];
        }
    }
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<bool> used(candidates.size(), false);
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0, used);
        return res;
    }
};

四、LeetCode131.分割回文串

https://leetcode.cn/problems/palindrome-partitioning/description/

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

示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]

提示:
1 <= s.length <= 16
s 仅由小写英文字母组成

带注释版:

class Solution {
private:
    vector<vector<string>> result;
    vector<string> path; // 放已经回文的子串
    void backtracking (const string& s, int startIndex) {
        // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
        if (startIndex >= s.size()) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < s.size(); i++) {
            if (isPalindrome(s, startIndex, i)) {   // 是回文子串
                // 获取[startIndex,i]在s中的子串
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
            } else {                                // 不是回文,跳过
                continue;
            }
            backtracking(s, i + 1); // 寻找i+1为起始位置的子串
            path.pop_back(); // 回溯过程,弹出本次已经填在的子串
        }
    }
    bool isPalindrome(const string& s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }
public:
    vector<vector<string>> partition(string s) {
        result.clear();
        path.clear();
        backtracking(s, 0);
        return result;
    }
};

优化后的代码就没详细学了

我能自己写出这个题的代码,已经觉得自己真的进步很大了:

class Solution {
private:
    vector<string> path;
    vector<vector<string>> res;

    bool isPalinedromic(string s, int begin, int end) {
        for (int i = begin, j = end; i < j; i++, j--) {
            if (s[i] != s[j]) return false;
        }
        return true;
    }

    void backtracking (string s, int startIndex) {
        if(startIndex > s.size()) return ;
        if (startIndex == s.size()) {
            res.push_back(path);
            return ;
        }
        for(int i = startIndex; i < s.size(); i++) {
            if(isPalinedromic(s, startIndex, i)) {
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
            } else continue;
            backtracking(s, i + 1);
            path.pop_back();
        }
    }
public:
    vector<vector<string>> partition(string s) {
        backtracking(s, 0);
        return res;
    }
};

今日总结:

题目确实是当天写完的,只是博客没有及时写

  1. 感觉到自己真的进步好多了,能够理解懂视频的题目讲解,经历一些debug后还能尝试控制时间的前提下独立完成代码。

  1. 今天的难点应该挺多的,我花了很多时间理解来着,讲解回文串分割的视频应该看了两遍。

  1. 另外今天的组合数一个不太容易想到题解的“去重”问题,也是挺困扰,希望真的掌握后能做到举一反三呢

继续加油哈小赵~

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STL——stack

一、stack介绍和使用 1.stack文档介绍 &#xff08;1&#xff09;stack是一种容器适配器&#xff0c;专门用于具有后进先出操作的上下文环境中&#xff0c;其只能从容器的一端进行元素的插入与删除&#xff0c;以及提取操作。 &#xff08;2&#xff09;stack是作为容器适配…

Python如何安装模块,python模块安装失败的原因以及解决办法

前言 今天来给刚开始学习python的朋友讲解一下 如何安装python模块, python模块安装失败的原因以及解决办法 很多朋友拿到代码之后&#xff0c;就开始复制粘贴 --> 然后右键进行运行 结果就是报错说 没有这个模块 得安装啥的 Python模块安装 一. 打开命令提示符 win …

分布式项目-品牌管理(5、6)

【今日成果】&#xff1a; //使用阿里云OSS服务&#xff1a; //使用v-if如果地址没有就不显示 &#xff0c; 如果地址错误图片就显示不出来&#xff1b; 【快速回顾】&#xff1a; 任何数据的删除都不要使用物理上的删除&#xff0c;应当使用逻辑上的删除&#xff01;&…

2023,AI 技能学起来

这段时间&#xff0c;ChatGPT 算是火出圈了。不仅朋友圈转发各种 ChatGPT 的文章&#xff0c;连我夫人这种从来不关注科技新闻的人也问我 ChatGPT 是怎么回事。其实我算是比较早知道并关注 ChatGPT 的&#xff0c;去年 12 月份还写了一篇文章 AI 也会写代码了&#xff0c;但我并…

一文讲清chatGPT的发展历程、能力来源和复现它的关键之处

1. ChatGPT是什么 chatGPT是什么&#xff1f;这可能是最近被问的最多的一个。 大家第一反应这应该是GPT系列的一个最新模型&#xff0c;普通大众可能更愿意把它看做是一个人工智能。实际上&#xff0c;它其实就是一个基于大规模语言模型的对话系统产品。官网对它定义十分的明…

前端——周总结系列四

1 JS变量与常量 概述 变量&#xff1a;在后续编码过程中会被重新赋值&#xff0c;是不断变化的。常量&#xff1a;固定不变的数据&#xff0c;日常生活比如性别男&#xff0c;代码层面是在编码过程中不会变化的固定数据。 命名规则 变量 可以包含数字&#xff0c;字母&…

四家文档管理系统中披露的未修补安全漏洞

四家供应商 LogicalDOC、Mayan、ONLYOFFICE 和 OpenKM 的开源和免费增值文档管理系统 (DMS) 产品中披露了多个未修补的安全漏洞。 佛山市东联科技有限公司网络安全研究人员表示&#xff0c;这八个漏洞提供了一种机制&#xff0c;“攻击者可以通过该机制说服人类操作员在平台上…

背包问题代码合集(C/C++)

目录 1 01背包问题 2 完全背包问题 3 多重背包问题 4 分组背包问题 1 01背包问题 有N件物品和一个容量是V的背包。每件物品只能使用一次。 第 物品的体积是&#xff0c;价值是。 求解将哪些物品装入背包&#xff0c;可使这些物品的总体积不超过背包容量&#xff0c;…

找数字(字符串思维题)

刚开始自己的思路&#xff1a;用dfs暴搜&#xff0c;没考虑时间复杂度&#xff08;准确来说当时不知道该题的时间复杂度该怎么算&#xff09;&#xff0c;如果用暴搜写的话是&#xff08;10的100次方&#xff09; 正确的思路&#xff1a;最小值&#xff1a;定义一个长度为m的空…

(C语言)指针进阶

问&#xff1a;1. ( )&#xff0c;[ ]&#xff0c;->&#xff0c;&#xff0c;--&#xff0c;. &#xff0c;&#xff0a;的操作符优先级是怎么样的&#xff1f;2. Solve the problems&#xff1a;只有一个常量字符串与一个字符指针&#xff0c;该怎么打印常量字符串所有内容…

AI_Papers:第一期

2023.02.06—2023.02.12 文摘词云 Top Papers Subjects: cs.CL 1.Multimodal Chain-of-Thought Reasoning in Language Models 标题&#xff1a;语言模型中的多模式思维链推理 作者&#xff1a;Zhuosheng Zhang, Aston Zhang, Mu Li, Hai Zhao, George Karypis, Alex Sm…

生产Nginx现大量TIME-WAIT,连接耗尽,该如何处理?

背景说明&#xff1a; 在尼恩读者50交流群中&#xff0c;是不是有小伙伴问&#xff1a; 尼恩&#xff0c;生产环境 Nginx 后端服务大量 TIME-WAIT &#xff0c; 该怎么办&#xff1f; 除了Nginx进程之外&#xff0c;还有其他的后端服务如&#xff1a; 尼恩&#xff0c;生产环境…

【设计模式-11】责任链模式

认识设计模式&#xff08;十一&#xff09;---责任链模式【一】责任链模式【二】介绍&#xff08;1&#xff09;意图&#xff08;2&#xff09;主要解决&#xff08;3&#xff09;何时使用&#xff08;4&#xff09;如何解决&#xff08;5&#xff09;关键代码&#xff08;6&am…

面对日益强大的AIGC,内容创作者们该何去何从?

面对日益强大的AIGC&#xff0c;内容创作者们该何去何从&#xff1f; 忽如一夜东风来&#xff0c;AIGC红全球。好似在一夜之间&#xff0c;AIGC就突然走红了&#xff0c;朋友圈、各大平台、各大社群&#xff0c;对于它的讨论话题也是越来越多。AIGC也成为了继NFT、元宇宙、Web…

pandas——groupby操作

Pandas——groupby操作 文章目录Pandas——groupby操作一、实验目的二、实验原理三、实验环境四、实验内容五、实验步骤一、实验目的 熟练掌握pandas中的groupby操作 二、实验原理 groupby(byNone, axis0, levelNone, as_indexTrue, sortTrue, group_keysTrue, squeezeFalse&…

STM32单片机GSM短信自动存取快递柜

实践制作DIY- GC0104-自动存取快递柜 一、功能说明&#xff1a; 基于STM32单片机设计-自动存取快递柜 二、功能介绍&#xff1a; STM32F103C系列最小系统板0.96寸OLED显示器DY-SV17F串口语音播报模块4*4矩阵键盘GSM短信模块4路舵机&#xff08;模拟4个柜子&#xff09; ***…

动规规划-完全背包问题

有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i]&#xff0c;得到的价值是value[i] 。每件物品都有无限个&#xff08;也就是可以放入背包多次&#xff09;&#xff0c;求解将哪些物品装入背包里物品价值总和最大。 首先回顾一下0-1背包问题&#xff0c;它和…

【寒假day4】leetcode刷题

&#x1f308;一、选择题❤1.下列哪一个是析构函数的特征&#xff08; &#xff09;。A: 析构函数定义只能在类体内 B: 一个类中只能定义一个析构函数 C: 析构函数名与类名相同 D: 析构函数可以有一个或多个参数答案&#xff1a;B答案解析&#xff1a;析构函数是构造函…

练 习

1.判断三个中最重的//依次输入相应的人的体重double people1, people2, people3;cout << "请输入第一个人体重" << endl;cin >> people1;cout << "请输入第二个人体重" << endl;cin >> people2;cout << "请…

开发人员与测试人员关系的理解

在软件开发中都会有开发人员&#xff08;以下简称开发&#xff09;和测试人员&#xff08;以下简称测试&#xff09;&#xff0c;在一些小型公司可能并没有测试&#xff0c;仅仅是开发兼任测试。在这里我仅针对于有专业的测试和专业的开发的项目。 每个公司应该都有考核机制&am…