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 ...
随机推荐
- Detours的使用准备
Detours是微软开发的一个函数库,可用于捕获系统API.在用其进行程序开发之前,得做一些准备工作: 一.下载Detours 在http://research.microsoft.com/sn/de ...
- Java-Class-C:java.util.HashMap
ylbtech-Java-Class-C:java.util.HashMap 1.返回顶部 1.1. import java.util.HashMap; import java.util.Map; 1 ...
- jquery判断对象是undifined,判断对象是null
判断对象是undifined: var aaa = undefined; if (typeof(aaa) == "undefined") { ... } typeof 返回的是字符 ...
- taskFactory
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- docker 错误failed to open stream: Permission denied 解决方法
在服务器上面.运行docker时,php目录会发生权限问题解决方法如下: 1:进入php目录下面 docker exec -ti php56 /bin/bash #进入php容器 chown -R w ...
- 零基础入门学习python--第一章
知识点汇总1. Python的应用范围:操作系统.3D动画.WEB.企业应用.云计算等.2. Python是什么类型的语言?脚本语言,即电脑编程语言,比C.C++或java之类的系统编程语言简单容易. ...
- c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)
一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...
- Windows服务调试状态下用Console启动
最近一直在用服务,发现服务也没有那么难调试. Windows服务调试状态下用Console启动:步骤分两步 第一步改Program,启动代码 static class Program { /// &l ...
- RAKsmart新出香港服务器的优势
RAKsmart为了更好地服务用户,所以最近RAKsmart新推出得香港服务器又带给了用户更多的选择,那这次RAKsmart新推出香港服务器有哪些优势呢? 1.带宽更大可升至10Mpbs 香港服务器的 ...
- CCPC-WFinal-女生专场
1001:CCPC直播 字符串处理,几个if语句 1002:口算训练 前缀和处理<=根号n的因数,大于根号n的因数每个数至多有一个,用vector存下每个大因数的位置,map离散化.查询 ...