力扣-回溯-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;
    }
};
相关推荐
CoovallyAIHub1 天前
工业视觉检测:多模态大模型的诱惑
深度学习·算法·计算机视觉
Jayden_Ruan1 天前
C++分解质因数
数据结构·c++·算法
bubiyoushang8881 天前
MATLAB实现雷达恒虚警检测
数据结构·算法·matlab
wu_asia1 天前
编程技巧:如何高效输出特定倍数数列
c语言·数据结构·算法
AlenTech1 天前
207. 课程表 - 力扣(LeetCode)
算法·leetcode·职场和发展
练习时长一年1 天前
LeetCode热题100(杨辉三角)
算法·leetcode·职场和发展
lzllzz231 天前
bellman_ford算法
算法
栈与堆1 天前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
sunfove1 天前
麦克斯韦方程组 (Maxwell‘s Equations) 的完整推导
线性代数·算法·矩阵
Rui_Freely1 天前
Vins-Fusion之 SFM准备篇(十二)
人工智能·算法·计算机视觉