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;
    }
}
相关推荐
CHEEVEN_QY26 分钟前
液冷板热阻计算与流阻优化的实战方法
人工智能·算法·机器学习
C++ 老炮儿的技术栈1 小时前
不一样的数值交换
数据结构·c++·人工智能·算法·c·csdn开发云
hahaha60162 小时前
Shades-of-Gray (SoG) 算法--白平衡算法
人工智能·嵌入式硬件·算法·计算机视觉
青山木2 小时前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
夊3 小时前
C语言第十一课:指针(五)
算法
小师兄吃牛肉3 小时前
C语言指针进阶全解析
java·c语言·算法
梦帮科技3 小时前
vLLM / TensorRT-LLM 极限推理:PagedAttention 细粒度物理页表管理与连续批处理(Continuous Batching)实战
数据结构·人工智能·分布式·python·深度学习·算法·vllm
做运维的阿瑞4 小时前
多表查询基础:INNER JOIN、LEFT JOIN、RIGHT JOIN
数据库·sql·mysql·算法
大侠归来5 小时前
C语言素数判断:从入门到进阶
java·c语言·算法
v:PLLucky205 小时前
农作物病虫害检测识别系统
深度学习·scrapy·算法·百度·微信公众平台