[抄题]:

给定一个字符串s,将s分割成一些子串,使每个子串都是回文串。

返回s所有可能的回文串分割方案。

给出 s = "aab",返回

[
["aa", "b"],
["a", "a", "b"]
]

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 主函数中要记得新建partition数组
  2. 字符串取长度需要加括号.length()
  3. j的起始位置是length - 1 保证有数,最后的结束条件是i < j,二者相邻
  4. 回溯法反复递归调用的是helper函数 对下一组进行操作, remove的是partition.size() - 1

[二刷]:

  1. List<List<String>>也必须用arraylist来实现
  2. 取子数组的方法是.substring()简称sb
  3. 要检验新数组是否有效,若不是回文字符串则退出

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

valid函数变成了回文串判断函数

[复杂度]:Time complexity: O(宽度的深度次方) Space complexity: O(宽度*深度)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

DFS,找出所有方法

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

分割回文串 II · Palindrome Partitioning II -DP

[代码风格] :

public class Solution {
/*
* @param s: A string
* @return: A list of lists of string
*/
public List<List<String>> partition(String s) {
//corner case
List<List<String>> result = new ArrayList<>();
List<String> partition = new ArrayList<>();
if (s == null || s.length() == 0) {
return result;
}
helper(s, partition, 0, result);
return result;
}
//helper
private void helper (String s, List<String> partition,
int startIndex, List<List<String>> result) {
if (startIndex == s.length()) {
result.add(new ArrayList<String>(partition));
return ;
} for (int i = startIndex; i < s.length(); i++) {
String sb = s.substring(startIndex, i + 1);
if (!isPalindrome(sb)) {
continue;
}
partition.add(sb);
helper(s, partition, i + 1, result);
partition.remove(partition.size() - 1);
}
}
//isPalindrome
private boolean isPalindrome (String s) {
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}

分割回文串 · Palindrome Partitioning的更多相关文章

  1. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  2. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

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

  3. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  4. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  5. Leetcode 132.分割回文串II

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...

  6. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  7. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

  8. [LeetCode] 132. 分割回文串 II

    题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...

  9. Java实现 LeetCode 132 分割回文串 II(二)

    132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...

随机推荐

  1. Tornado源码分析之http服务器篇

    转载自 http://kenby.iteye.com/blog/1159621 一. Tornado是什么? Facebook发布了开源网络服务器框架Tornado,该平台基于Facebook刚刚收购 ...

  2. javascript节点操作appendChild()

    cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...

  3. EL中拼接字符串的方法

    近期在项目中碰到一个需要在JSP页面中比较两String类型的值的问题,于是想当然的写了如下代码: <c:if test="${'p'+longValue=='p1'}"&g ...

  4. 【BZOJ】1257: [CQOI2007]余数之和(除法分块)

    题目 传送门:QWQ 分析 大佬和我说本题是除法分块,莫比乌斯反演中也有用到. QwQ我不会莫比乌斯反演啊~ 题目让我们求  $ \sum_{i=1}^n  k\mod n $ 然后根据$ a \mo ...

  5. Hadoop2.0构成之HDFS2.0

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  6. ETL,BPM与ESB三者的一些感悟

    1.ETL: 数据层之间,主要在数据库层面上进行数据抽取过程------数据库层 2.ESB 异构系统之间通过总线技术,实现系统交互---------------系统通信层 3.BPM 自动化流程处理 ...

  7. InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活

    InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活 1.Lincense Server激活 // 激活IDEA的License Server 地址 ...

  8. Bogart BogartPublic.vb

    Imports System.Data.SqlClient Imports System.Data #Region "IBogartToolbar,請勿隨便更改" Interfac ...

  9. python编程之禅

    在python界面输入 import this >>> import this The Zen of Python, by Tim Peters Beautiful is bette ...

  10. 学习笔记:Zepto笔记

    1.Zepto对象不能自定义事件 例如执行:$({}).bind('cust',function(){}); 结果:TypeError:Object#hasnomethod'addEventListe ...