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;
    }
}
相关推荐
CoovallyAIHub31 分钟前
首个大规模、跨模态医学影像编辑数据集,Med-Banana-50K数据集专为医学AI打造(附数据集地址)
深度学习·算法·计算机视觉
熬了夜的程序员32 分钟前
【LeetCode】101. 对称二叉树
算法·leetcode·链表·职场和发展·矩阵
却道天凉_好个秋1 小时前
目标检测算法与原理(二):Tensorflow实现迁移学习
算法·目标检测·tensorflow
柳鲲鹏2 小时前
RGB转换为NV12,查表式算法
linux·c语言·算法
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——串联所有单词的字串
数据结构·算法·c/c++
Kuo-Teng2 小时前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
mit6.8242 小时前
[HDiffPatch] 补丁算法 | `patch_decompress_with_cache` | `getStreamClip` | RLE游程编码
c++·算法
程序猿20232 小时前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法
葵续浅笑2 小时前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode