Palindrome Partitioning
Palindrome Partitioning
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"]
]
参考了这个锅锅的,思路很清晰,用的类似DFS递归实现http://www.sjsjw.com/kf_code/article/033776ABA018056.asp
import java.util.ArrayList;
import java.util.List; public class Solution { public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<List<String>>(); //存放结果
List<String> curList = new ArrayList<String>(); //记录当前结果 if(s.length() == 0)
return result;
getResult(result, curList, s); return result;
} /**
* 判断字符串是否为回文
* @param str
* @return
*/
public boolean isPalindrom(String str){
for(int i = 0, j = str.length() - 1; i < j; i++, j--){
if(str.charAt(i) != str.charAt(j))
return false;
} return true;
} /**
* 递归调用
* @param curList
* @param str
*/
public void getResult(List<List<String>> result,List<String> curList, String str){
if(str.length() == 0)
result.add(new ArrayList<String>(curList)); //满足条件添加到结果集中,注意这里因为curList是引用,必须new一个新的list出来
else{
for(int i = 1; i <= str.length();i++){
String head = str.substring(0, i);
if(isPalindrom(head)){ //子串是回文
curList.add(head);
getResult(result, curList, str.substring(i));
curList.remove(curList.size() - 1);
}
}
}
}
// public void show(List<List<String>> list){
// for(List<String> list_temp : list){
// for(String string_temp : list_temp){
// System.out.print(string_temp + " ");
// }
// System.out.println();
// }
// }
}
Palindrome Partitioning的更多相关文章
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LintCode Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Palindrome Partitioning II Leetcode
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- 初识 Asp.Net内置对象之Session对象
Session对象 Session对象用于存储在多个页面调用之间特定用户的信息.Session对象只针对单一网站使用者,不同的客户端无法相互访问.Session对象中止联机机器离现时,,也就是当网站使 ...
- Oracl用代码建标
建标还可以通过编写代码的方式实现,这样在建许多类似的表的时候可以极高建表的效率. create table SCORE --建立表名( ...
- CSS - 实现文字显示过长时用省略
一.添加-文字显示超出范围时隐藏属性 overflow:hidden; 二.添加-超出文字省略号属性 text-overflow:ellipsis; 三.添加-文字不换行属性 white-space: ...
- HTML5+开发移动app-mui开发示例
mui 官网:http://dcloudio.github.io/mui/ 说明:希望你可以把官网内容通读一遍,这是以后开发的基础 开始 新建项目 在首页点击新建移动App,如下: 或者在项目管理器内 ...
- .NET下实现分布式缓存系统Memcached (转自网络)
Memcached在.NET中的应用 一.Memcached服务器端的安装(此处将其作为系统服务安装) 下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, ...
- 三.CSS层叠机制
概述 层叠就是对样式的层叠.是某种样式在样式表中逐层叠加的过程.让浏览器对某个标签特定属性值的多个来源,最终确定使用那个值.层叠是整个CSS的核心机制. HTML文档样式的来源 1.浏览器默认样式,每 ...
- Java之公约、公倍数
约数和倍数:若整数a能够被b整除,a叫做b的倍数,b就叫做a的约数.公约数:几个数公有的约数,叫做这几个数的公约数:其中最大的一个,叫做这几个数的最大公约数. 那么,我们用编程来看公约数和公倍数: p ...
- Cocos2d-JS中的cc.LabelTTF
cc.LabelTTF是使用系统中的字体,它是最简单的标签类.cc.LabelTTF类图如下图所示,可以cc.LabelTTF继承了cc.Node类,具有cc.Node的基本特性. LabelTTF类 ...
- 苹果在Xcode6中弃用segue的push和model
苹果在Xcode6中弃用了segue的push和model.被show和present取代. 下面是新版本中每种类型的使用总结和例子.建议自己使用前先在测试项目中自己试一试 Show 根据当前屏幕中的 ...
- org.springframework.util.Assert
方法入参检测工具类 Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回. 类似的,当我们在编写类的方法时,也常常需要对方法入参进行合 法性检查,如果入参不符合 ...