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

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

相关推荐
海洲探索-Hydrovo1 小时前
TTP Aether X 天通透传模块丨国产自主可控大数据双向通讯定位模组
网络·人工智能·科技·算法·信息与通信
2401_841495644 小时前
【计算机视觉】基于复杂环境下的车牌识别
人工智能·python·算法·计算机视觉·去噪·车牌识别·字符识别
Jonkin-Ma4 小时前
每日算法(1)之单链表
算法
晚风残5 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
杨云强5 小时前
离散积分,相同表达式数组和公式
算法
地平线开发者5 小时前
征程 6 | BPU trace 简介与实操
算法·自动驾驶
满天星83035775 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
Lris-KK6 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
麦麦鸡腿堡6 小时前
Java的动态绑定机制(重要)
java·开发语言·算法
zy_destiny6 小时前
【工业场景】用YOLOv8实现抽烟识别
人工智能·python·算法·yolo·机器学习·计算机视觉·目标跟踪