leetcode — palindrome-partitioning
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的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
随机推荐
- c++ a+b
#include<iostream> using namespace std; int main() { int a,b,sum; cin>>a>>b; sum=a ...
- python基础知识总结(一)
学完python很久了,一直想着写个学习总结,奈何懒癌晚期,现在才开始写.以下是我总结的一小部分python基础知识点的总结: 1.什么是解释型语言?什么是编译型编程语言? ''' 解释型语言:无需编 ...
- Redis配置参数详解
Redis配置参数详解 /********************************* GENERAL *********************************/ // 是否作为守护进 ...
- Flutter 编写内联文本
使用Text.rich或者RichText ListView( children: <Widget>[ Text.rich( TextSpan( text: 'Text: ', child ...
- cmd 创建用户,并授权管理员权限就可以远程登陆了
创建账号 net user 用户名 密码 /add //注意空格 授权管理员权限 net localgroup Administrators 用户名 /add // ...
- kvm虚拟机克隆
1.先关闭被克隆的虚拟机: 2.克隆命令 virt-clone -o 192.168.0.242_sw_web -n 192.168.0.163_nginx -f /data/kvm/images/1 ...
- 使用bind提供域名解析服务搭建
正向解析实验 1.安装bind服务 2.在/etc目录中找到该服务程序的主配置文件,然后把第11行和第17行的地址均修改为any 3.正向解析参数如下: 4.编辑数据配置文件,从/var/named目 ...
- 两层fragment嵌套时出现空白,(收藏别人的)
完美解决 两层Fragment,内层空白 转载:http://blog.csdn.net/bingospunky/article/details/51352400 目录(?)[+] 前言 两层Frag ...
- leetcode-数组-子集
一.题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], ...
- Spring使用支付宝扫码支付
前一段一直在研究支付宝的扫码支付,不得不说,支付宝的文档写的真是一个烂(起码在下刚开始看的时候是mengbi的).文档上面的示例和demo里面的示例长的完全不一样.往往文档上面的例子很简单,而demo ...