分割回文串 · 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 解释: 进行一 ...
随机推荐
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- 1016 Phone Bills (25 分)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...
- Oracle中分页查询和联表查询
1.使用ROWNUM伪列查询 1.1.查询十条数据(rownum<=n) SELECT ROWNUM,A.* FROM v_sjjx_unit_info A WHERE ROWNUM<=1 ...
- 用 tornado 做网站 (7)
转自:http://wiki.jikexueyuan.com/project/start-learning-python/309.html 用 tornado 做网站 (7) 到上一节结束,其实读者已 ...
- spring的Ioc容器与AOP机制
为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...
- CSS3 圆角属性 border-radius和-webkit-border-radius使用
CSS3 圆角属性 border-radius 在 CSS3 中新增了一个 border-radius 边框半径属性,即大家常用的圆角效果.这使得制作圆角将不再麻烦,只需对所用对象加一个 border ...
- sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
在华为emotion ui 2.0自带的浏览器中,给部分组件设置了圆角后会发现背景仍然是方的,内部边框是圆的, 对于这种bug, 只需在对应的设置圆角的css样式中加入 background-clip ...
- git 隐藏文件删除
1.首先切换到当前目录 cd /Users/wlm/Desktop/XXX/XXX 2.执行下面的命令: defaults write com.apple.finder AppleShowAllFil ...
- 进行web开发时应该考虑的架构性因素
功能实现 这个自不必说. 性能与可伸缩性 根据预期的访问量,评估机器负载情况.如果在可预期的未来一台服务器可以撑得住,则没必要使用多台服务器.需要对多个环节进行性能评估:web服务器.逻辑服务器.DB ...
- 《内存数据库和mysql的同步机制》
如下图