【LeetCode热题100】【回溯】分割回文串

题目链接:131. 分割回文串 - 力扣(LeetCode)

要找出所有分割这个字符串的方案使得每个子串都是回文串,写一个判断回文串的函数,深度遍历回溯去找出所有分割方案,判断分割的子串是否是回文串

复制代码
class Solution {
public:
    vector<vector<string> > ans;
    vector<string> an;
    string s;

    bool isPalindromes(int left, int right) {
        while (left < right) {
            if (s[left++] != s[right--])
                return false;
        }
        return true;
    }

    void dfs(int i) {
        if (i == s.size()) {
            ans.push_back(an);
            return;
        }
        for (int j = i; j < s.size(); ++j) {
            if (isPalindromes(i, j)) {
                an.push_back(s.substr(i, j - i + 1));
                dfs(j + 1);
                an.pop_back();
            }
        }
    }

    vector<vector<string> > partition(string s) {
        this->s = move(s);
        dfs(0);
        return ans;
    }
};
相关推荐
core51215 分钟前
PageRank 算法:互联网的“人气投票”
算法·pagerank
小白菜又菜19 分钟前
Leetcode 1523. Count Odd Numbers in an Interval Range
算法·leetcode
你们补药再卷啦1 小时前
人工智能算法概览
人工智能·算法
cnxy1881 小时前
围棋对弈Python程序开发完整指南:步骤3 - 气(Liberties)的计算算法设计
python·算法·深度优先
AndrewHZ1 小时前
【图像处理基石】什么是光栅化?
图像处理·人工智能·算法·计算机视觉·3d·图形渲染·光栅化
小白菜又菜1 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode
我找到地球的支点啦2 小时前
Matlab系列(006) 一利用matlab保存txt文件和读取txt文件
开发语言·算法·matlab
Dev7z2 小时前
基于Matlab实现GRACE卫星重力数据的全球水储量变化估算与分析
人工智能·算法·matlab
爱喝热水的呀哈喽2 小时前
11题目汇总
算法
三斗米3 小时前
Transformer入门:一文读懂《Attention Is All You Need》
算法·架构