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的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  2. [LeetCode] Palindrome Partitioning 拆分回文串

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

  3. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  4. LintCode Palindrome Partitioning II

    Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...

  5. LeetCode(131)Palindrome Partitioning

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

  6. Leetcode 131. Palindrome Partitioning

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

  7. Palindrome Partitioning II Leetcode

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

  8. 【leetcode】Palindrome Partitioning II(hard) ☆

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

  9. [Leetcode] Palindrome Partitioning

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

  10. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

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

随机推荐

  1. WebAPI GET和POST请求的几种方(转发)

    WebAPI GET和POST请求的几种方式 GET请求 1.无参数get请求 一般get请求有两种写法,一种是$.get()   一种是$.ajax({type:"get"}), ...

  2. 用c#写一个json的万能解析器

    CommonJsonModel .cs /// <summary> /// 万能JSON解析器 /// </summary> public class CommonJsonMo ...

  3. link_mysql的php版

    <?php $str_sql_read="select count(*) as num from userinfo"; $str_sql_del="delete f ...

  4. 根据不同的浏览器对不同元素进行css调整

    <!if firefox> .element { top:4px; } <![endif]> <!if chrome> .element { top:6px; } ...

  5. LINQ简介和LINQ to SQL语句之Where

    LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感 ...

  6. WIN7实用的运行命令

    运行命令主要是DOS操作系统的运行方式.为方便用户的操作,微软公司将一些常用的命令,如DIR,CD等命令全部集成在系统里面:存放这些内部命令的文件是“Command”(文件后缀.com).它与IO.s ...

  7. Jsp万能密码漏洞修复例子

    更多详细内容请查看:http://www.111cn.net/jsp/Java/58610.htm 如果网站出现这种“万能密码”漏洞该怎么办呢 'or'='or' 漏洞修复 方法有很多在这里介绍两种, ...

  8. Java学习之Java的单例模式

    单例模式有一下特点: 1.单例类只能有一个实例.2.单例类必须自己自己创建自己的唯一实例.3.单例类必须给所有其他对象提供这一实例. 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个 ...

  9. ASP.NET 使用 System.Web.Script.Serialization 解析 JSON (转)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  10. PHP的接口(interface)

    接口声明了函数和字段,但不会给出实现的细节 规则: 1.类全部为抽象方法(不需要声明abstract) 2.接口抽象方法必须是public 3.成员(字段)必须是常量 interface Comput ...