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的更多相关文章

  1. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  2. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  3. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  5. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  6. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  7. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. leetcode 131. Palindrome Partitioning----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. ionic:ionic 教程

    ylbtech-ionic:ionic 教程 1.返回顶部 1. ionic 教程 ionic 是一个强大的 HTML5 应用程序开发框架(HTML5 Hybrid Mobile App Framew ...

  2. 20140422 ALT+F8 四个强制类型转换

    一.static_cast, dynamic_cast, const_cast http://www.cnblogs.com/chio/archive/2007/07/18/822389.html h ...

  3. 20140320 roc曲线 sizeof

    1.roc曲线 http://www.zhizhihu.com/html/y2012/4076.html 2.using namespace std的缺点:程序中定义一个变量cout会被误认为是std ...

  4. C#中ORM的简单实现

    ORM在功能上主要有两个: 把从数据库中查询返回的DataSet,DataTable转化为我们可以方便使用的实体类集合: 把要对数据库操作的实体类集合或条件转化为数据库可以直接执行的SQL语句.

  5. <读书笔记>如何入门爬虫?

    大部分爬虫框架都是 发送请求 获得页面 解析页面 下载内容 存储内容 定个宏伟目标 淘宝1000页 知乎 豆瓣 ... python基础 list.dict:序列化爬取的内容 切片:分割爬取内容,获取 ...

  6. Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets

    Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...

  7. Java Lambda map返回部分属性

    通过map,返回部分属性. MyUser,作为源数据 MyUserS,作为返回的新数据. @Test public void Test1() { List<MyUser> ulist=ne ...

  8. Android开发 多媒体提取器MediaExtractor详解_将一个视频文件分离视频与音频

    前言 此篇博客讲解MediaExtractor将一个视频文件分离视频与音频,如果你对MediaExtractor还没有一个笼统的概念建议先了解我的另一篇入门博客:https://www.cnblogs ...

  9. 解决vagrant上使用Homestead很慢(响应速度10s+)

    说明: 使用vagrant和Homestead 在vBox上面跑laravel, 响应速度非常缓慢(大概在10+s), 尝试过增加虚拟机配置, 但是没有任何效果, 经验证也不是数据库的原因 . 通过网 ...

  10. Xshell 排版错乱问题