• 【Leetcode】 131. 分割回文串


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

    回文串 是正着读和反着读都一样的字符串。

    示例 1:

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

    示例 2

    输入s = "a"
    输出[["a"]]

    提示:

    1 <= s.length <= 16
    
    • 1

    s 仅由小写英文字母组成


    AC:

    /*
     * @lc app=leetcode.cn id=131 lang=cpp
     *
     * [131] 分割回文串
     */
    
    // @lc code=start
    class Solution {
    private:
        vector<string> path;
        vector<vector<string>> result;
        bool isPalidrome(string& s, int start, int end) {
            for(int i = start, j = end; i < j; i++, j--)
            {
                if(s[i] != s[j])
                    return 0;
            }
            return 1;
        }
        void backtracking(string& s, int startIndex) {
            if(startIndex == s.size())
            {
                result.push_back(path);
                return ;
            }
            for(int i = startIndex; i < s.size(); i++) {
                if(isPalidrome(s, startIndex, i)) {
                    string str = s.substr(startIndex, i - startIndex + 1);
                    path.push_back(str);
                    backtracking(s, i + 1);
                    path.pop_back();
                }
            }
            return ;
        }
    public:
        vector<vector<string>> partition(string s) {
            result.clear();
            path.clear();
            backtracking(s, 0);
            return result;
        }
    };
    // @lc code=end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    AC
    第一次提交的时候,出现了没使用&引用类型,但是通过了?
    有没有大佬帮忙解释下,不使用&浪费的空间资源以及其底层逻辑?

    /*
     * @lc app=leetcode.cn id=131 lang=cpp
     *
     * [131] 分割回文串
     */
    
    // @lc code=start
    class Solution {
    private:
        vector<string> path;
        vector<vector<string>> result;
        bool isPalidrome(string s, int start, int end) {
            for(int i = start, j = end; i < j; i++, j--)
            {
                if(s[i] != s[j])
                    return 0;
            }
            return 1;
        }
        void backtracking(string s, int startIndex) {
            if(startIndex == s.size())
            {
                result.push_back(path);
                return ;
            }
            for(int i = startIndex; i < s.size(); i++) {
                if(isPalidrome(s, startIndex, i)) {
                    string str = s.substr(startIndex, i - startIndex + 1);
                    path.push_back(str);
                    backtracking(s, i + 1);
                    path.pop_back();
                }
            }
            return ;
        }
    public:
        vector<vector<string>> partition(string s) {
            result.clear();
            path.clear();
            backtracking(s, 0);
            return result;
        }
    };
    // @lc code=end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    AC

    另外附上,最接近一段时间学习回溯算法的总结:

    void backtracking(参数) {
        if (终止条件) {
            存放结果;
            return;
        }
    
        for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
            处理节点;
            backtracking(路径,选择列表); // 递归
            回溯,撤销处理结果
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ------------ 诸君,共勉! ---------------

  • 相关阅读:
    DCDC电源电流定义
    [科普文] Web3 中的资产负债表
    不要和自己的大脑抗争,将大脑的能耗降到最低
    九号公司——高科技黑马的进击与困境
    基于java的学生考勤信息管理系统设计【附源码】
    Flowable(一个开源的工作流和业务流程管理引擎)中与事件相关的一些核心概念
    【随想】每日两题Day.3(实则一题)
    自从外包干了四年,基本废了...
    VMware 虚拟机图文安装和配置 AlmaLinux OS 8.6 教程
    神经网络-卷积神经网络案例详解
  • 原文地址:https://blog.csdn.net/qq_54053990/article/details/133523003