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. 《转》python 9 字典,numpy

    http://www.cnblogs.com/BeginMan/p/3156960.html 一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的关系. ...

  2. NEERC 1999 Advertisement /// oj22646

    题目大意: 输入k,n :k为每位慢跑者最少应看到的广告牌数 接下来n行 描述第 i 位慢跑者的途径路段 输出需要设立的广告牌数 接下来每行为设立地点 Sample Input 5 101 1020 ...

  3. Shell 学习(二)

    目录 Shell 学习(二) 1 设置环境变量 1.1 基本语法 1.2 实践 2 位置参数变量 2.1 介绍 2.2 基本语法 2.3 位置参数变量应用实例 3 预定义变量 3.1 基本介绍 3.2 ...

  4. JS事件 鼠标单击事件( onclick )通常与按钮一起使用。onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行

    鼠标单击事件( onclick ) onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用. 比如,我们单击按钮时,触发  ...

  5. Face-Resources

    Face-Resources Following is a growing list of some of the materials I found on the web for research ...

  6. Benchmark of Large-scale Unconstrained Face Recognition-blufr 算法的理解

    Many efforts have been made in recent years to tackle the unconstrained face recognition challenge. ...

  7. 家庭-养老院模型理解IOC和DI

    控制反转 IOC 1. 概念 应用内部不负责依赖对象的创建和维护, 由第三方负责, 这样控制权就由应用内部转移到外部容器, 控制权的转移就是所谓的反转. 2. 比喻 有一户家庭(应用)有个老人(依赖对 ...

  8. delphi判断MDI窗体的子窗体是否存在

    转]delphi判断MDI窗体的子窗体是否存在//*************************************************************************** ...

  9. C#获取当前运行的源代码的文件名和当前源代码的行数的方法

    在C#中记录日志时,为了以后查找错误或者跟踪的方便,最好能记录下出错的源代码的文件名和出错的源代码的行数. 这2个方法如下: /// <summary>         /// 取得当前源 ...

  10. 关于Async与Await的FAQ

    =============C#.Net 篇目录============== 环境:VS2012(尽管System.Threading.Tasks在.net4.0就引入,在.net4.5中为其增加了更丰 ...