131. 分割回文串

力扣链接:. - 力扣(LeetCode)

复制代码
class Solution {
    List<List<String>> ans = new ArrayList<>();
    List<String> temp = new ArrayList<>();
    String s;
    public List<List<String>> partition(String s) {
        this.s = s;
        dfs(0);
        return ans;
    }
    void dfs(int st) {
        if(st == s.length()) {
            ans.add(new ArrayList(temp));
            return ;
        }
        //注意,substring是左闭右开,所以是i<=s.length()
        for(int i=st+1;i<=s.length();i++) {
            String now = s.substring(st, i);
            if(check(now)) {
                temp.add(now);
                dfs(i);
                temp.remove(temp.size()-1);
            }
        }
    }

    boolean check(String str) {
        int len = str.length();
        for(int i=0;i<len/2+1;i++){
            if(str.charAt(i)!=str.charAt(len-i-1)){
                return false;
            }
        }
        return true;
    }
}
相关推荐
让我们一起加油好吗24 分钟前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗1 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
lisw0512 小时前
稀疏算法概述!
算法·机器人·无人机
YLCHUP13 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴14 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu18 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu19 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
凌冰_20 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏21 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
饮水桶_T1 天前
C++ STL00 | 六大核心组件协作框架 笔记
开发语言·算法·百度·facebook·新浪微博