博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode][JAVA] Palindrome Partitioning
阅读量:4361 次
发布时间:2019-06-07

本文共 1018 字,大约阅读时间需要 3 分钟。

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",

Return

[    ["aa","b"],    ["a","a","b"]  ] 回溯算法,需要注意的就是在得到子串时判断是否为回文字符串(需要自己写函数),如果是才继续展开下一层。 代码:
1 public List
> partition(String s) { 2 List
> re = new ArrayList
>(); 3 if(s==null || s.length()==0) 4 return re; 5 List
path = new ArrayList
(); 6 collect(re, path, s, 0); 7 return re; 8 } 9 public void collect(List
> re, List
path, String s, int start)10 {11 for(int i=start+1;i<=s.length();i++)12 {13 String temp = s.substring(start,i);14 if(!isP(temp))15 continue;16 path.add(temp);17 if(i==s.length())18 re.add(new ArrayList
(path));19 else20 collect(re, path, s, i);21 path.remove(path.size()-1);22 }23 }24 public boolean isP(String x)25 {26 int p = 0;27 int q = x.length()-1;28 while(p

 

 

转载于:https://www.cnblogs.com/splash/p/4051772.html

你可能感兴趣的文章
windows命令——explorer
查看>>
<转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列...
查看>>
jquery和js cookie的使用解析
查看>>
类的内置方法
查看>>
世界是数字的 读后感
查看>>
算法项目步骤流程
查看>>
POJ 2942 Knights of the Round Table ★(点双连通分量+二分图判定)
查看>>
10.scheam.xml的配置
查看>>
Android Studio 生成aar包多Module引用问题
查看>>
hdu--1540 Tunnel Warfare(线段树+区间合并)
查看>>
通过命令给Linux(CentOS)分区
查看>>
Sprint1规划暨first stand up meeting
查看>>
python接口自动化3-自动发帖(session)
查看>>
复杂问题的简单抽象:魔兽世界中的兔子们
查看>>
那些美到极致的语言!
查看>>
Xamarin的不归路-ios模拟器没有键盘
查看>>
【云笔记】群晖DS218+ NoteStation 折腾
查看>>
jdk安装配置
查看>>
四、RocketMq简单的消费者和生产者(示例代码)
查看>>
json介绍
查看>>