【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(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

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

相关推荐
聚客AI12 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v14 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工16 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农17 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了18 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo18 小时前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法