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

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

相关推荐
Jasmine_llq1 分钟前
《B3939 [GESP样题 四级] 绝对素数》
数据结构·算法·素数判断算法·数字拆分与反转算法·区间遍历枚举·双条件判断逻辑
郝学胜-神的一滴17 分钟前
干货版《算法导论》07:递归视角下的选择排序与归并排序
java·数据结构·c++·python·程序人生·算法·排序算法
csdn_aspnet25 分钟前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
shehuiyuelaiyuehao31 分钟前
多线程入门
java·python·算法
Navigator_Z33 分钟前
LeetCode //C - 1073. Adding Two Negabinary Numbers
c语言·算法·leetcode
醇氧40 分钟前
【OpenClaw】更换阿里百炼完整配置指南
算法·ai
Tina学编程1 小时前
[HOT100]每日一练------最长连续序列
算法·hot 100
csdn_aspnet1 小时前
PHP 算法 LeetCode 编号 70 - 爬楼梯
算法·leetcode·php
沈浩(种子思维作者)1 小时前
没有错误,正确将一文不值
人工智能·python·算法·量子计算
x_xbx1 小时前
LeetCode:5. 最长回文子串
算法·leetcode·职场和发展