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. CentOS 启动提示unexpected inconsistency;RUN fsck MANUALLY, ntfs的input/output Error,InPageError c000009c使用chkdsk修复磁盘,12款Linux系统恢复工具

    CentOS这两天服务器出了问题了,提示如下: unexpected inconsistency;RUN fsck MANUALLY An error occurred during the file ...

  2. AtCoder ABC 132E Hopscotch Addict

    题目链接:https://atcoder.jp/contests/abc132/tasks/abc132_e 题目大意 给定一张 N 个点 M 条边无自环无重边的一张有向图,求从起点 S 能否三步三步 ...

  3. Python匹马行天下之面向对象

    概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...

  4. mysql实现访问审计

    mysql的连接首先都是通过init_connect初始化,然后连接到实例. 我们利用这一点,通过在init_connect的时候记录下用户的thread_id,用户名和用户地址实现db的访问审计功能 ...

  5. Docker学习のWindows下安装Docker

    一.docker最初只支持linux的,因此在windows下运行需要虚拟机. 利用VirtualBox建立linux虚拟机,在linux虚拟机中安装docker服务端和客户端 利用Windows的H ...

  6. fping简介及使用方法

    fping命令的官网为:http://fping.org/ 历史版本下载地址:http://fping.org/dist/ 第一步:安装. wget http://fping.org/dist/fpi ...

  7. [NOIP2005] 过河【Dp,思维题,缩点】

    Online Judge:Luogu P1052 Label:Dp,思维题,缩点,数学 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子 ...

  8. Python全栈开发:django网络框架(二)

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  9. css----7渐变

    linear-gradient(90deg,red 10%,yellow 20%,green 30%) <!DOCTYPE html> <html> <head> ...

  10. CF982F The Meeting Place Cannot Be Changed

    题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?n<=100005,m<=500005. 标程: #include< ...