Palindrome Partitioning

时间:2022-09-23 14:44:42

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. &lbrack;LeetCode&rsqb; Palindrome Partitioning II 拆分回文串之二

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

  2. &lbrack;LeetCode&rsqb; Palindrome Partitioning 拆分回文串

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

  3. Leetcode&colon; 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&period; 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&lpar;hard&rpar; &star;

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

  9. &lbrack;Leetcode&rsqb; Palindrome Partitioning

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

  10. 19&period; Palindrome Partitioning &amp&semi;&amp&semi; Palindrome Partitioning II (回文分割)

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

随机推荐

  1. DOM编程 删除节点

    需求: 为每个 li 节点添加一个 confirm(确认对话框): 确定要删除 xx 的信息吗?若确定, 则删除 1,获取li所有节点 var liNodes = document.getElemen ...

  2. 【CodeVS 2822】爱在心中

    “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有着一个 ...

  3. Python 基礎 - 認識模塊

    什麼是模塊?簡單說就是別人寫好了一堆功能,封裝在一起. 模塊有分二種,一個是之前有提到的 標準庫,就是不需要透過額外的安裝就有的模塊 ,另一個叫 第三方庫,需要另外安裝才能使用的模塊 #!/usr/b ...

  4. iOS - Swift NSNumber&Tab;&Tab;数字

    前言 public class NSNumber : NSValue public class NSDecimalNumber : NSNumber NSNumber 可以被赋值为各种数值类型.我们可 ...

  5. 使用Visual Studio 2013进行单元测试--初级篇

    1.打开VS2013 --> 新建一个项目.这里我们默认创建一个控制台项目.取名为UnitTestDemo 2.在解决方案里面新增一个单元测试项目.取名为UnitTestDemoTest 创建完 ...

  6. Oracle中SQL调优(SQL TUNING)之最权威获取SQL执行计划大全

    该文档为根据相关资料整理.总结而成,主要讲解Oracle数据库中,获取SQL语句执行计划的最权威.最正确的方法.步骤,此外,还详细说明了每种方法中可选项的意义及使用方法,以方便大家和自己日常工作中查阅 ...

  7. event工具集

    eventTool = { // 页面加载完成后 readyEvent : function(fn) { if (fn==null) { fn=document; } var oldonload = ...

  8. Javascript arguments&period;callee和caller的区别

    一.callee 在学习callee之前,需要先学习arguments. arguments: 含义:该对象代表正在执行的函数和调用它的函数的参数. 语法: 1 [function.]argument ...

  9. 目标检测-ssd

    intro: ECCV 2016 Oral arxiv: http://arxiv.org/abs/1512.02325 paper: http://www.cs.unc.edu/~wliu/pape ...

  10. pycharm中导入requests,xmlx等模块的方法。

    现在pycharm的功能越来越强大,我们需要什么直接就可以导入: 下面正式开始介绍: 第一步: 先选择左边的:project interpreter   ----->再点击后面的绿色的加号. 那 ...