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 ...
随机推荐
- codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 在实现 ...
- 代码研磨 Slim v3 (一)--app->get()&route->add()
index.php代码如下: $app->get('/forbase', function ($request, $response, $args){ Example\Module\Base:: ...
- sql的基本用法-------修改字段默认值和属性
修改表中已有的字段属性 ALTER TABLE 表名 ALTER COLUMN 字段名 varchar(500) --sqlserver建表表时设置字段的默认值 create table 表(id i ...
- C#之父 Anders Hejlsberg
Anders Hejlsberg (born December 1960)[2] is a prominent Danish software engineer who co-des ...
- 一个页面多Table多分页的问题
一个页面有多个table,多个pagination,一个pagerForm.这种情况下怎么解决多个pagination不同pagenum的问题呢? 如果是这样的滴话,使用局部刷新就可以了,两个tabl ...
- SQL 复制订阅 异常后 强制删除
最近做数据库同步备份工作,将 主库 通过SQLService 自带的 [复制] 订阅出去后,因为 订阅方(从库) 发生异常,主库 无法确定 从库的订阅,就想清理了,订阅重新做同步,结果.....主库上 ...
- SQLSERVER中按年月分组
SQLSERVER中按年月分组 一个表有三个字段id,dt,d 分别存放id,时间,数值 id dt d 1 2004-08-11 12:12:00.000 9 2 2005-09- ...
- UI2_UICollectionViewPicture
// AppDelegate.m // UI2_UICollectionViewPicture // // Created by zhangxueming on 15/7/16. // Copyrig ...
- php29号小结(隔行换色······)
1.给表单加样式,可以用css.php与js.js这三种,其中最好用php与js,现在很推广用这种. css3样式隔行换色(table中) tr:nth-child(even){ background ...
- BeanDefinition的Resource定位——3
1.我们重点看看AbstractRefreshableApplicationContext的refreshBeanFactory方法的实现,这个refreshBeanFactory被FileSyste ...