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 ...
随机推荐
- jieba库
Note of Jieba ( 词云图实例 ) Note of Jieba jieba库是python 一个重要的第三方中文分词函数库,但需要用户自行安装. 一.jieba 库简介 (1) jieba ...
- Logstash 6.4.3 导入 csv 数据到 ElasticSearch 6.4.3
本文实践最新版的Logstash从csv文件导入数据到ElasticSearch. 本文目录: 1.初始化ES.Kibana.Logstash 2.安装logstash文件导入.过滤器等插件 3.配置 ...
- JS区分对象类型
Object.prototype.toString.call() 区分对象类型 在JavaScript中数据类型分为:1.基本类型,2.引用类型 基本类型:Undefined,Boolean,Stri ...
- java DTO对象与PO对象的相互转换
2018-09-27 10:27:50 前言: 在实际开发中往往需要DTO对象与PO对象的相互转换: 先说说什么是DTO对象吧,个人觉得DTO就是PO的扩展而已,PO专门指向数据库,DTO作扩展(字段 ...
- Flutter 编写内联文本
使用Text.rich或者RichText ListView( children: <Widget>[ Text.rich( TextSpan( text: 'Text: ', child ...
- 严重: A child container failed during start
四月 20, 2019 4:54:28 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler [&qu ...
- 基于Jmeter的thrift-RPC接口测试
根据需求,产品部分功能采用thrift-RPC协议进行接口的增.删.改.查,前期采用Junit对其进行测试,为了提高RPC接口测试的简洁化和后期的性能测试需求,打算通过Jmeter的java类测试实现 ...
- Update API
Update API可以根据提供的脚本更新文档. 该操作从索引获取文档,运行脚本(脚本语言和参数是可选的),并返回操作的结果(也允许删除或忽略该操作). 使用版本控制来确保在“get”(查询文档)和“ ...
- linux 使用sh@d0ws0cks client
Linux Centos7下安装使用Shadowsocks客户端,实现*** 准备 SS: 搭建一个可以连接外网的服务器 教程可见 自己动手搭梯子 服务器:本人用的腾讯云服务器,系统为Centos7 ...
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
作者:林蓝东 最近的一个手机 QQ 版本发出去后收到比较多关于 CoreMotion 的 crash 上报,案发现场如下: 但是看看这个堆栈发现它完全不按照套路出牌啊! 乍一看是挂在 CoreMoti ...