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. Linux多核并行编程关键技术

    多核并行编程的背景 在摩尔定律失效之前,提升处理器性能通过主频提升.硬件超线程等技术就能满足应用需要.随着主频提升慢慢接近撞上光速这道墙,摩尔定律开始逐渐失效,多核集成为处理器性能提升的主流手段.现在 ...

  2. BZOJ2649 : riddle

    题意同3495,但是内存限制收紧了,不能采用3495的前后缀优化建图的方式. 注意到“每个集合恰好选择一个点”可以放宽成“每个集合最多选择一个点”,对于最后求出的方案里,如果某个集合没选点,任选一个就 ...

  3. Pycharm安装+python安装+环境配置

    Pycharm 工具: 1.安装jdk(64位):jdk-8u65-windows-x64.exe 路径:C:\Program Files\Java(默认路径) 2.配置环境 步骤一: 系统变量→新建 ...

  4. linux系统资源监控

    top命令 1.平均负载(load average): 正在耗费CPU进程与正在等待io的进程之和,三个值分别是一分钟,五分钟,十五分钟的平均负载,负载值只要小于CPU颗粒数属于正常情况 任务进程(T ...

  5. Mybatis_4.接口类和XML同时使用

    1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...

  6. SQL server SELECT 语句的基本结构

    SELECT select_list [INTO new_table] [FROM table_source] [WHERE search_condition] [GROUP BY GROUP_BY_ ...

  7. select标签默认选项

    1.selected:默认选择该选项: 2.disabled:该选项不能被鼠标选择:(注:选项没有被隐藏的时候) 3.style="display:none":隐藏该选项:(注:该 ...

  8. 关于css中为什么要设置html和body的高度?

    1.在怪异模式下,也就是网页的头部不写DOCTYPE的时候,body作为根元素,设置高度为百分百的时候.可以是页面的高度和浏览高度相同,在标准模式下也就是有DOCTYPE的时候,html才是根元素这时 ...

  9. Ooui.Wasm:浏览器中的.NET

    小型.NET Web框架Ooui作为Web程序集(WASM)在浏览器中完全运行 – 不需要服务器端的交互,涉及的所有文件都直接从浏览器执行,.NET Web框架Ooui可以作为其开发人员Frank A ...

  10. webpack 4.0 中 clean-webpack-plugin 的使用

    其实 clean-webpack-plugin 很容易知道它的作用,就是来清除文件的. 一般这个插件是配合 webpack -p 这条命令来使用,就是说在为生产环境编译文件的时候,先把 build或d ...