给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]
1 <= s.length <= 16s 仅由小写英文字母组成- class Solution {
- public List
> partition(String s){
- List
> res=new ArrayList<>();
- Deque
path=new ArrayDeque<>(); - dfs(s,0,path,res);
- return res;
- }
- private void dfs(String s,int begin,Deque
path,List> res)
{ - if(begin>=s.length()){
- res.add(new ArrayList<>(path));
- return;
- }
- for(int i=begin;i
-
- if(isPalindome(s,begin,i)){
- String str=s.substring(begin,i+1);
- path.addLast(str);
-
- }else{
- continue;
- }
- dfs(s,i+1,path,res);
- path.removeLast();
- }
- }
- private boolean isPalindome(String s,int start,int end){
- for(int i=start,j=end;i
- if(s.charAt(i)!=s.charAt(j)){
- return false;
- }
- }
- return true;
- }
- }
-
相关阅读:
makefile之VPATH和vpath的用法
【Vue面试专题】50+道经典Vue面试题详解!
国内CRM软件系统厂商排名
switch&循环语句
ssm+教务信息管理 毕业设计-附源码161124
进程与线程
分布式事务协调中间件---seata快速入门
solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
CSS引入样式的方式
weapp-tailwindcss for uni-app 样式条件编译语法插件
-
原文地址:https://blog.csdn.net/kt1776133839/article/details/128189781