分割回文串 II · Palindrome Partitioning II
[抄题]:
给定一个字符串s,将s分割成一些子串,使每个子串都是回文。
返回s符合要求的的最少分割次数。
[思维问题]:
不知道要用预处理字符串降低复杂度
[一句话思路]:
先把预处理获得s中回文串的结果放在数组中,之后直接调用
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 长区间依赖于短区间:先循环长度,再循环起点。递推关系:当前回文分割=下一字回文分割&当前字母。要有返回函数。
- 需要计算s.charAt(i + 1)时,上限是s.length() - 1
- 字符串取长度要加括号s.length()
- 从0开始,最后一位是i-1 递归调用的判断条件是isPalindrome[j][i - 1]
[二刷]:
- 要弄明白函数表达的目的。比如判断是不是回文串,就返回一个boolean数即可。
- isPalindrome[j][i - 1]表示从第j 位到最后一位,而不是反之
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
f[i] = i - 1写成n - 1 了,临时变量不要写成上限
[总结]:
第0位拿来初始化。因此数组多加一位,最后一位也要处理,边界变成<=
[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2) 二维数组
降低查询回文判断的方法:预处理,存在数组中,选i j,为n^2。查询数组为1
[英文数据结构或算法,为什么不用别的数据结构或算法]:
DP最少方法
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
95. Unique Binary Search Trees II DP,虽然不是切单词
[代码风格] :
public class Solution {
/**
* @param s a string
* @return an integer
*/
//isPalindrome
private boolean isPalindrome(String s, int start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
//getIspalindrome
private boolean[][] getIspalindrome(String s) {
boolean[][] isPalindrome = new boolean[s.length()][s.length()];
for (int i = 0; i < s.length(); i++) {
isPalindrome[i][i] = true;
}
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
isPalindrome[i][i + 1] = true;
}
else isPalindrome[i][i + 1] = false;
}
for (int length = 2; length < s.length(); length++) {
for (int start = 0; start + length < s.length(); start++) {
isPalindrome[start][start + length] = isPalindrome[start + 1][start + length - 1] &&
s.charAt(start) == s.charAt(start + length);
}
}
return isPalindrome;
}
public int minCut(String s) {
//corner case
if (s == null || s.length() == 0) {
return 0;
}
//state
boolean[][] isPalindrome = getIspalindrome(s);
int[] f = new int[s.length() + 1];
//initialization
for (int i = 0; i <= s.length(); i++) {
f[i] = i - 1;
}
//function
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (isPalindrome[j][i - 1]) {
f[i] = Math.min(f[i], f[j] + 1);
}
}
}
//answer
return f[s.length()];
}
};
分割回文串 II · Palindrome Partitioning II的更多相关文章
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- [LeetCode] 132. 分割回文串 II
题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 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 ...
随机推荐
- sklearn 线性模型使用入门
LinearRegression fits a linear model with coefficients to minimize the residual sum of squares betw ...
- BCGcontrolBar(一) MFC界面库简介
原帖地址:http://blog.csdn.net/zw514159799/article/details/9148385 英文原文:http://www.bcgsoft.com/bcgcontrol ...
- javascript创建对象之构造函数模式(二)
对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...
- 慕课网价值149《前端JavaScript面试技巧》笔记大公开——适应群体(学生或应届毕业生)
1.基础知识(一) http://note.youdao.com/noteshare?id=b81f56399b01da0ab5e870ea612ab94b&sub=B8ECBC1B57154 ...
- string hashcode 解读
偶尔看到string hashcode方法如下 public int hashCode() { int h = hash; if (h == 0 && value.length > ...
- idea-activate code
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- 9.简单理解ajax
#### post 请求需要发送一个header setRequestHeader('Content-Type','application/x-www-form-urlencoded') post请求 ...
- spring boot 整合 (全)
参考: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
- jpa-spring -basic
applicationContent.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- spring 注解 @NotBlank and BingResult
@NotEmpty用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 在 user对象中需要