力扣-回溯-131 分割回文串

思路

需要判断回文串,再选择是否添加到path里

代码

cpp 复制代码
class Solution {
public:
    vector<string> path;
    vector<vector<string>> result;
    bool isReverse(string s){
        int l = 0, r = s.size() - 1;
        while(l < r){
            if(s[r] != s[l]){
                return false;
            }
            r--;
            l++;
        }
        return true;
    }

    void backTracking(string s, int statIndex){
        if(statIndex == s.size()){
            result.push_back(path);
            return;
        }

        string sub;
        for(int i = statIndex; i<s.size(); i++){
            sub += s[i];
            if(!isReverse(sub)) continue;
            path.push_back(sub);
            backTracking(s, i+1);
            path.pop_back();
        }

        return;
    }
    vector<vector<string>> partition(string s) {
        backTracking(s, 0);

        return result;
    }
};
相关推荐
mmmmath_35 小时前
LeetCode.199.二叉树的右视图
数据结构·leetcode
专注仿真5 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
码爸5 小时前
支持向量机介绍与示例
算法·机器学习·支持向量机
怕浪猫5 小时前
2028编程圈很火的10个Skills
算法·面试·github
Nil20810 小时前
leetcode 198打家劫舍
算法·leetcode·职场和发展
Shaoshing10 小时前
前缀和与差分
java·数据结构·算法·前缀和·差分
mmmmath_310 小时前
LeetCode.429.N叉树的层序遍历
数据结构·leetcode
loulanyue_11 小时前
构建房产认知决策智能:Harness底座与AI自进化的技术实践——读贝壳首席算法科学家尹俊希2026云栖专场演讲
人工智能·算法
碧海银沙音频科技研究院11 小时前
基于炬芯ATS录音卡BLE控制协议
人工智能·嵌入式硬件·算法·语音识别