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. Sublime Text—自带快捷键介绍

    前言: Sublime Text是个小巧便捷的编辑器,除了众多好用的插件外,还有它自带的快捷键,打代码事半功倍,不会用的赶紧看看吧! 其实菜单上都有,看不懂可以汉化,Key Bindings-Defa ...

  2. .net导出Word的一种方法

    由于ActiveX控件只支持IE(好像FF可以通过安装插件支持),所以js导出word的方式就比较局限 可是如果当页面经过js修改以后,.net是无法获取到的,所以要通过js获取到最新的html并传给 ...

  3. C#_枚举类型

     C#中的枚举是名/值对的数据类型,下面是自定义的军衔等级的枚举 //定义枚举 enum MilitaryRank { Commander, ArmyCorpCommander, Military ...

  4. Part 12 DateTime functions in SQL Server

    DateTime functions in SQL Server IsDate, Day, Month, Year and DateName DateTime functions in SQL Ser ...

  5. .net 后台中对html标签按钮跳转后台以及后台简单验证

    ---------------------------------学霸,学神,大牛,hacker请绕道de分割线-------------------------------------------- ...

  6. SQL语句统计每天的数据

    按用户注册时间统计每天注册的不同来源.不同状态的用户数量: ), RegisterTime, ) RDate ,--DATEPART(YEAR, RegisterTime) 年 ) END 'AWai ...

  7. OpenGL第15,16,17讲小结

    这三讲没有什么特别的感觉,15讲把纹理贴到3D字体上,16讲讲了雾的生成,17讲是通过以事先保存好的纹理贴图的字体来显示2D文字,这样子不用调用windows的字体,而是使用纹理中的字体.这样也在一定 ...

  8. sicily 1022. Train Problem

    本题主要是出栈入栈顺序的问题 Home Problems My Status Standing <->           1022. Train Problem     Total: 2 ...

  9. 基类和派生类--this

    基类指针在程序运行的时候的确指向的是一个派生类的对象,但指针的类型仍然是基类指针.C++是一种强类型语言,因此不能用基类指针类型的指针直接调用派生类:而且,同一个类可能有多种不同的派生类,因此不知道实 ...

  10. Microsoft.Xna.Framework.TitleContainer.OpenStream()

    /// <summary> /// This method opens a file using System.IO classes and the /// TitleLocation p ...