力扣131题分割回文串C++--一文搞懂手推一遍递归树

文章介绍了如何使用回溯算法来分割给定字符串成回文串子串。通过递归函数`backtracking`,结合`isPalidrome`函数判断子串是否为回文,逐步构建并回溯解决方案。在每次递归中,将当前回文子串添加到路径`path`,然后继续探索剩余部分,最后得到所有可能的回文子串组合。
摘要由CSDN通过智能技术生成

刚刚接触回溯有点懵逼,发现手写一遍思路会清晰很多。
题目链接在这里:分割回文串

废话不多说,直接看手推:
在这里插入图片描述
看懂递归的过程很容易写出如下代码:
主要代码如下:

class Solution {
public:
    vector<vector<string>> result;
    vector<string> path;
    void backtracking(const string & s, int startIndex){
        // s代表输入的字符串
        // 终止条件
        if(startIndex >= s.size()){
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i<s.size(); i++){
            // 判断是否是回文字符
            if(isPalidrome(s, startIndex, i)){ // 如果是回文字符串:加入path
                // 从startIndex开始到结尾的子串
                // 比如“abcd”, 当startIndex为0时候:for循环有三次:"a", "ab", "abc", "abcd"。
                // 这里回顾一下string容器中的子串操作函数:substr(int pos = 0, int n = pos) const;
                // 返回由pos开始的n个字符组成的字符串(注意n是获取字符串个数)如下操作
                // 获取[startIndex, i]的字符串
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
                //
            }else{
                // 如果不是,结束本次循环,直接continue
                continue;
            }
            // 递归,选择了i+1为起始位置的子串>比如:这时候startIndex为1:
            // 进入递归下一层的for循环就是从1-2子串:"b", "bc", "bcd"这三个情况判断
            // 这里建议手推一遍
            backtracking(s, i + 1);
            path.pop_back(); // 这里要回溯
        }
    }
    // 判断是否是回文字符串的函数
    bool isPalidrome(const string& s, int start, int end){
        // start开头字符, end结尾字符
        // 这个就是双指针法,同时从头和尾巴出进行比较,直到左指针等于或者大于右指针
        for(int i = start, j = end; i<j; i++, j--){
            if(s[i] != s[j]){
                return false;
            }
        }
        return true;
    }
    vector<vector<string>> partition(string s) {
        result.clear();
        path.clear();
        backtracking(s, 0);
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值