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 ...
随机推荐
- 2019 牛客多校第一场 A Equivalent Prefixes
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题目大意 定义 RMQ(u, L, R) 为 u 数组在区间 [L, R] 上最小值的下标. 如果有 2 ...
- Python服务端工程师就业面试指导✍✍✍
Python服务端工程师就业面试指导 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时 ...
- python学习8—函数之高阶函数与内置函数
python学习8—函数之高阶函数与内置函数 1. 高阶函数 a. map()函数 对第二个输入的参数进行第一个输入的参数指定的操作.map()函数的返回值是一个迭代器,只可以迭代一次,迭代过后会被释 ...
- USACO2005 Cow Acrobats /// 前缀和 oj23402
题目大意: N (1 ≤ N ≤ 50,000)头牛叠罗汉 找到最优排序使所有牛的风险值总和最小 每头牛重量 (1 ≤ Wi ≤ 10,000) 力量 (1 ≤ Si ≤ 1,000,000,000) ...
- 【2018ACM/ICPC网络赛】徐州赛区
呃.自闭了自闭了.我才不会说我写D写到昏天黑地呢. I Characters with Hash 题目链接:https://nanti.jisuanke.com/t/31461 题意:给你一个字符串 ...
- css布局-瀑布流的实现
一.基本思路 1.先看最终的效果图: 2.实现原理:通过position:absolute(绝对定位)来定位每一个元素的位置,并且将当前列的高度记录下来方便下一个dom位置的计算 二.代码实现 1.版 ...
- JDBC_数据库连接池DRUID
/** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/26 20:12 */ /* 1.数据库连接池: 1. ...
- mysql 03章_完整性、约束
.完整性:数据库中数据的可靠性有效性和合理性我们称为数据的完整性,这样才能保证数据合理符合现实生活中的数据体现. 注:数据完整性的设计应该在设计表的时候就进行设计了,而不是等到数据库中已经存在数据才进 ...
- shell 脚本 功能性语句 read,expr,test
1. read 从终端输入数据赋值给变量 read val 把读入的数据存放到val中 #!/bin/bash echo -n "please input two number :" ...
- NLP杂点
1.停用词 stop words: 在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词. 停用词都是人工输入.或者由一个停用词表导入. 2.jieba是目前最好的 Python 中文分词组件 ...