leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs

解法1:
递归
观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome
那么挑选cut的位置就很有意思,后一次cut可以建立在前一次cut的基础上
举例来说
"aab"
第一次cut"a" "ab"
第二次cut"a" "b"
很容易总结出规律,s(i, j)被i=<k<j,截断
若s(i,k)本身就是palindrome,那么我们只需要将s(i,k) 与s(k+1, j)的所有满足题目要求的子串组合,分别组合即可。
怎么求这些子串组合呢?递归解即可
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if(s.length() == 0)
return res;
if(s.length() == 1){
res.add(Arrays.asList(s));
return res;
}
for(int i=0; i<s.length(); i++){
int x=0, y=i;
boolean preIsP = true;
while(x < y){
if(s.charAt(x++) != s.charAt(y--)){
preIsP = false;
break;
}
}
if(preIsP){
List<List<String>>laterRes = partition(s.substring(i+1));
String pre = s.substring(0, i+1);
for(int j=0; j < laterRes.size(); j++){
List<String> tmp = new ArrayList<>(laterRes.get(j));
tmp.add(0, pre);
res.add(tmp);
}
if(i == s.length()-1){
List<String> tmp = new ArrayList<>();
tmp.add(0, pre);
res.add(tmp);
}
}
}
return res;
}
}
解法2:
dp解
这里要用到两个dp
一个一维pre[],表达s(0~i-1)所有满足题目的要求的结果
一个二维dp[][],表达s(i~j)是否为一个palindrome
更新方程:
若i和j相等,则需要看子串i+1~j-1是否为palindrome
当j-i<=1时,不存在子串,所以要考虑这种情况,加上一个||
if(s.charAt(i) == s.charAt(j)) && (dp[i+1][j-1] || j-i <=1)
dp[i][j] == true
若s(i~j)已经是palindrome了,那么只需要把s(i~j)和所有满足条件的s(0~i-1)结果组合即可得到答案
所以要遍历pre[i]
把所有 {pre[i]中存放的结果 + s(i~j) }加入pre[i+1]
最后返回pre(s.length());
class Solution {
public List<List<String>> partition(String s) {
List<List<String>>[] pre = new List[s.length()+1];
pre[0] = new ArrayList<List<String>>();
pre[0].add(new ArrayList<String>());
boolean[][] isP = new boolean[s.length()][s.length()];
for(int i=0; i<s.length(); i++){
pre[i+1] = new ArrayList<List<String>>();
for(int j=0; j<=i; j++){
if(s.charAt(i) == s.charAt(j) && (i-j <= 1 || isP[j+1][i-1])){
isP[j][i] = true;
String str = s.substring(j, i+1);
for(List<String> tmp : pre[j]){
List<String> tmp2 = new ArrayList<>(tmp);
tmp2.add(str);
pre[i+1].add(tmp2);
}
}
}
}
return pre[s.length()];
}
}
leetcode 131 Palindrome Pairs的更多相关文章
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- leetcode 131. Palindrome Partitioning----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Java-Class-@I:org.springframework.web.bind.annotation.RestController
ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.RestController 1.返回顶部 2.返回顶部 1. pack ...
- Delphi中的Sender:TObject对象解析转载
https://blog.csdn.net/jl_tiny/article/details/24376661 Delphi中的Sender:TObject对象解析 procedure TForm1.B ...
- 简单实用的makefile
简单的makefile 为了说明问题,就新建一组文件如下: 文件布局及运行结果: make clean 按目录归置 文件看起来是是清楚了,但是makefile写得揪心. 实用版 (1)Makefile ...
- JVM基本知识总结
大概两三个月之前阅读了<深入理解Java虚拟机>(周志明著),也为了加深印象,这里简单的做下总结,想完整点了解JVM知识的也可以阅读本书,书写的不错,相当通俗易懂. 第一部分 内存管理机制 ...
- git mac安装
1.git安装包安装 去官网下载最行的git版本 安装即可 https://git-scm.com/download/mac 但是一般的git仓库需要sshkey来做验证 下面奉上具体的命令: 需要生 ...
- VBA中msgbox的用法小结
1.作用在消息框中显示信息,并等待用户单击按钮,可返回单击的按钮值(比如“确定”或者“取消”).通常用作显示变量值的一种方式.2.语法MsgBox(Prompt[,Buttons][,Title][, ...
- 两个table合并
1.两个一样的table合并用Merge函数即可合并(但要求table要有主键id) DataTable1.Merge(DataTable2); 2.没写完,以后继续补充(只有经过笔者验证,能用的才会 ...
- 【数论】[SDOI2010]古代猪文
大概就是求这个: $$G^\sum_{k|N} C_{n}^{k}$$ 显然只要把后面的$\sum_{k|N}C_{n}^{k}$求出来就好了 几个要用的定理: 欧拉定理的推论:(a和n互质) $$a ...
- GoDaddy商务主机建站具有的优势
GoDaddy是世界第一域名注册服务商,近年来凭借着优异的性能受到国内站长的欢迎,其中Godaddy商务主机得到了很多站长的喜爱,那么为什么GoDaddy商务主机可以受到那么多站长的喜爱呢?下面就带大 ...
- String 详解
String String对象不可变,当对象创建完毕之后,如果内容改变则会创建一个新的String对象,返回到原地址中. 不可变优点: 多线程安全. 节省空间,提高效率. 源码: public fin ...