【Leetcode】 131. 分割回文串

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

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

示例 1:

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

示例 2

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

提示:

cpp 复制代码
1 <= s.length <= 16

s 仅由小写英文字母组成


AC:

cpp 复制代码
/*
 * @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

第一次提交的时候,出现了没使用&引用类型,但是通过了?

有没有大佬帮忙解释下,不使用&浪费的空间资源以及其底层逻辑?

cpp 复制代码
/*
 * @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

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

cpp 复制代码
void backtracking(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
        处理节点;
        backtracking(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

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

相关推荐
Jerry11 分钟前
LeetCode 1047. 删除字符串中的所有相邻重复项
算法
可编程芯片开发3 小时前
基于霍尔传感器和PID控制器的有功功率检测控制系统simulink建模与仿真
算法
To_OC3 小时前
LC 17 电话号码的字母组合:我的回溯算法,就是从这道题开窍的
javascript·算法·leetcode
测试秃头怪5 小时前
如何评估自动化测试脚本的编写时间和维护工作量?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
战族狼魂5 小时前
GPT-5.6与Grok 4.5重磅发布
人工智能·算法·大模型·大语言模型
白日焰火16 小时前
基于 OpenSpec 实现规范驱动开发
算法·交互
imuliuliang6 小时前
关于图搜索算法的性能建模与可预测性研究7
算法
ForDreamMusk7 小时前
批量归一化
人工智能·算法·机器学习
Ivanqhz8 小时前
刚体的自由度
人工智能·算法
KaMeidebaby8 小时前
卡梅德生物技术快报|抗体合成:多肽抗体合成工程化方案:Nsp2 保守肽多抗制备与多维度验证
前端·网络·数据库·人工智能·算法