分割回文串 · Palindrome Partitioning
[抄题]:
给定一个字符串s,将s分割成一些子串,使每个子串都是回文串。
返回s所有可能的回文串分割方案。
给出 s = "aab"
,返回
[
["aa", "b"],
["a", "a", "b"]
]
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 主函数中要记得新建partition数组
- 字符串取长度需要加括号.length()
- j的起始位置是length - 1 保证有数,最后的结束条件是i < j,二者相邻
- 回溯法反复递归调用的是helper函数 对下一组进行操作, remove的是partition.size() - 1
[二刷]:
- List<List<String>>也必须用arraylist来实现
- 取子数组的方法是.substring()简称sb
- 要检验新数组是否有效,若不是回文字符串则退出
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼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的更多相关文章
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- [LeetCode] 132. 分割回文串 II
题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
随机推荐
- [UE4GamePlay架构(九)GameInstance(转)
GameInstance这个类可以跨关卡存在,它不会因为切换关卡或者切换游戏模式而被销毁.然而,GameMode和PlayController就会再切换关卡或者游戏模式时被引擎销毁重置,这样他们里面的 ...
- flume系统使用以及与storm的初步整合
Flume NG的简单使用可以参考介绍文档:http://blog.csdn.net/pelick/article/details/18193527,图片也来源此blog: 下载完fl ...
- Delphi Webbrowser使用方法详解(二)
delphi如何用webbrowser模拟登录网站? 我们就以如何登录博客园来做示例: 1.要登入一个网站,首先要获取网页的源代码,我们可以通过网页菜单--查看--查看源代码来获取. 2.我们找到登录 ...
- 管理11gRAC基本命令 (转载)
在 Oracle Clusterware 11g 第 2 版 (11.2) 中,有许多子程序和命令已不再使用: crs_stat crs_register crs_unregiste ...
- HTML5之viewport使用
好久都没更新博客了,最近一年转型移动端,当然网页端也得兼顾,慢慢写一写基本性的文章,多积累. 本期介绍下viewport的一些使用: 先看看viewport在页面中的样子: <meta name ...
- IDEA在编辑时提示could not autowire
IDEA在编辑时提示could not autowire 原创 2016年05月14日 10:53:38 28338 在开发中我再applicationContext-dao.xml中加入了mappe ...
- 关于 百度 Ueditor (在chrome浏览器) 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
- spring data jpa 注解
@Data 注解引出的 lombok 小辣椒 今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok, ...
- bean-json-bean-json 工具
package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExc ...
- MySQL5.7.20 二进制包 在Linux系统中的 安装和配置
01, 下载安装包 => https://dev.mysql.com/downloads/mysql/ 02, 上传到linux系统, 笔者这里安装在 root 目录下, 常见安装在 var/ ...