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

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

相关推荐
DoraBigHead1 小时前
小哆啦解题记——映射的背叛
算法
Heartoxx1 小时前
c语言-指针与一维数组
c语言·开发语言·算法
孤狼warrior2 小时前
灰色预测模型
人工智能·python·算法·数学建模
京东云开发者2 小时前
京东零售基于国产芯片的AI引擎技术
算法
chao_7893 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
十盒半价3 小时前
从递归到动态规划:手把手教你玩转算法三剑客
javascript·算法·trae
GEEK零零七3 小时前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战3 小时前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
秋说3 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
WebInfra3 小时前
如何在程序中嵌入有大量字符串的 HashMap
算法·设计模式·架构