leetcode — palindrome-partitioning-ii
import java.util.Arrays;
/**
*
* Source : https://oj.leetcode.com/problems/palindrome-partitioning-ii/
*
* Given a string s, partition s such that every substring of the partition is a palindrome.
*
* Return the minimum cuts needed for a palindrome partitioning of s.
*
* For example, given s = "aab",
* Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
*/
public class PalindromePartition2 {
/**
* 将字符串分割为多个回文字符串的最小分割次数
*
* 定义数组cut[len+1],cut[i]表示从0-的最小分割数,初始化cut[0] = -1,
* 当i-j是回文字符串的时候,cut[i] = min(cut[i], cut[j]+1)
*
* 最后得出的cut[len+1]就是0-(len+1)之间的最小分割数
*
* @param str
* @return
*/
public int minPartition (String str) {
if (str.length() <= 1) {
return 0;
}
int[] cut = new int[str.length()+1] ;
Arrays.fill(cut, Integer.MAX_VALUE);
boolean[][] table = new boolean[str.length()][str.length()];
cut[0] = -1;
for (int i = str.length()-1; i >= 0; i--) {
for (int j = i; j < str.length(); j++) {
if ((i+1 > j - 1 || table[i+1][j-1]) && str.charAt(i) == str.charAt(j)) {
table[i][j] = true;
}
}
}
for (int i = 1; i <= str.length(); i++) {
for (int j = i-1; j > -1; j--) {
if (table[j][i-1]) {
cut[i] = Math.min(cut[i], cut[j] + 1);
}
}
}
return cut[str.length()];
}
public static void main(String[] args) {
PalindromePartition2 palindromePartition2 = new PalindromePartition2();
System.out.println(palindromePartition2.minPartition("aab") + "==1");
}
}
leetcode — palindrome-partitioning-ii的更多相关文章
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- sudo命令详解
语法 sudo(选项)(参数) 选项 选项 说明 -b 在后台执行指令: -h 显示帮助: -H 将HOME环境变量设为新身份的HOME环境变量: -k 结束密码的有效期限,也就是下次再执行sudo时 ...
- 通过s3cmd上传css文件到s3导致样式加载失败
情景说明: 将css文件上传到aws s3存储桶中,通过浏览器访问页面,发现css文件渲染失败. 通过浏览器工程模式发现css为 Content-Type: text/html,正确的 Content ...
- Create and test an approval workflow with Microsoft Flow
https://docs.microsoft.com/zh-cn/flow/getting-started https://docs.microsoft.com/en-us/flow/modern-a ...
- RSA算法加解密
package org.thcic.ejw.util.encrypt; import java.io.ByteArrayOutputStream; import java.security.Key; ...
- typeScript 学习
最近看了下typescript, 虽然说已经有很多人已经用到它了,但是我还是写写自己的feel咯:这里推荐学习链接 https://ts.xcatliu.com. 这个入门学习,我不好做评价,但是我自 ...
- Android Studio 去除上方标题
选中代码再Ctrl+shift+'/' 可加/***/注释 https://blog.csdn.net/wuqingsen1/article/details/78554117 styles.xml & ...
- 关于using namespace std
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~关于using namespace std ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- Chrome+postman+postman interceptor调试
本文使用chrome+postman4.8.3+postman interceptor0.2.23调试使用cookie的请求. postman4.8.3下载地址:https://pan.baidu.c ...
- MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)
测试缘由 一个开发同事做了一个框架,里面主键是uuid,我跟他建议说mysql不要用uuid用自增主键,自增主键效率高,他说不一定高,我说innodb的索引特性导致了自增id做主键是效率最好的,为了拿 ...
- xss挖掘初上手
本文主要总结了xss可能出现的场景.偏向于案例,最后分享一哈简单的绕过和比较好用的标签. 1.搜索框 首先看能否闭合前面的标签. 如输入111”><svg/onload=alert(1)& ...