import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/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"]
* ]
*
*/
public class PalindromePartitioning { /**
* 将字符串进行任意位置的分割,找到分割后的子串都是回文字符串的结果
*
* 因为需要判断字符串的各个子串是不是回文字符串,所以可以使用longest palindrome substring中的动态规划
*
* 将每一次分割后的子串的判断结果记录下来
*
* 然后私用DFS寻找所有子串是回文的情形
*
* @param str
* @return
*/
public List<List<String>> partition (String str) {
boolean[][] table = new boolean[str.length()][str.length()];
for (int i = str.length()-1; i >= 0; i--) {
for (int j = i; j < str.length(); j++) {
if ((i+1 >= j-1 || table[i+1][j-1]) && str.charAt(i) == str.charAt(j) ) {
table[i][j] = true;
}
}
}
List<List<String>> result = new ArrayList<List<String>>();
List<String> patition = new ArrayList<String>();
findPartitions(str, 0, table, result, patition);
return result;
}
private void findPartitions (String str, int start, boolean[][] table, List<List<String>> result, List<String> partition) {
if (str.length() == start) {
result.add(new ArrayList<String>(partition));
return ;
}
for (int i = start; i < str.length(); i++) {
if (table[start][i]) {
partition.add(str.substring(start, i+1));
findPartitions(str, i + 1, table, result, partition);
partition.remove(partition.size()-1);
}
}
} private static void print (List<List<String>> list) {
for (List<String> strList : list) {
System.out.println(Arrays.toString(strList.toArray(new String[strList.size()])));
}
System.out.println();
} public static void main(String[] args) {
PalindromePartitioning palindromePartitioning = new PalindromePartitioning(); print(palindromePartitioning.partition("aab"));
}
}

leetcode — palindrome-partitioning的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

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

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

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

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

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

  5. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  6. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  7. LeetCode: Palindrome Partitioning 解题报告

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

  8. Leetcode: Palindrome Partitioning II

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

  9. [Leetcode] Palindrome Partitioning

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

  10. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

随机推荐

  1. 数据分析 大数据之路 四 numpy 2

    NumPy 数学函数 NumPy 提供了标准的三角函数:sin().cos().tan(import numpy as np a = np.array([0,30,45,60,90])print (' ...

  2. 面试题int和Integer

    int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...

  3. node04

    1.模板引擎 用于渲染页面 介绍jade或ejs jade:侵入式,与原生html/css不共存,使用缩进代表层级 模板后缀.jade ejs:则非侵入式的 2.jade 1)简单使用: //代码 c ...

  4. python批量提取eml附件

    从批量eml文件中提取附件,使用方式如下 代码如下 import email import os import sys #获取eml附件信息 def Get_Annex_Message(FilePat ...

  5. Bypass_Disable_functions_Shell

    Bypass_Disable_functions_Shell https://github.com/l3m0n/Bypass_Disable_functions_Shell 一个各种方式突破Disab ...

  6. 干掉safedog命令

    sc delete safedogguardcenter    shutdown -r -t 00 两条命令搞定

  7. HBuilder git合作-代码同步

    1. 以下场景的操作都是同样的,包括:新建了文件.删除了文件.独占式修改文件(即不存在多人同时修改一个文件的情况) 提交 项目修改完成后,选中项目,右键Team->Commit 一般是选择Com ...

  8. HashMap 相关面试题及其解答

    Q:HashMap 的数据结构? A:哈希表结构(链表散列:数组+链表)实现,结合数组和链表的优点.当链表长度超过 8 时,链表转换为红黑树. transient Node<K,V>[] ...

  9. [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  10. [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...