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 ...
随机推荐
- JavaScript(八)
闭包 什么是闭包 函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回 function aaa(a){ var b = 5; function bbb(){ a++; ...
- URI ,URL 和 URN
URI : 统一资源标识符,用来唯一标识互联网资源,包括URL和URN URL:统一资源定位器 包含: 协议,域名,端口,路由,参数,hash https://i.cnblogs.com/EditPo ...
- VB.NET或C#报错:You must hava a license to use this ActiveX control.
VB.NET或者C# winform开发时,如果使用了Microsoft Visual Basic 6.0 ActiveX,并动态创建该控件实例,那么程序移植到没有安装Visual Basic 6.0 ...
- .net 发布程序时出现“类型ASP.global_asax同时存在于...”错误的解决办法
web程序发布后,通过浏览器访问程序显示如下的错误信息: 编译器错误消息: CS0433: 类型“ASP.global_asax”同时存在于“c:\WINDOWS\Microsoft.NET\Fram ...
- VS启动Winform项目提示:不支持互操作调试
64 位平台不支持互操作调试(托管 + 非托管混合模式调试). 在VS中设置项目属性--->调试--->取消选中“启用本地代码调试”. 此问题在.NET FrameWork低版本框架会出现 ...
- react-native 打包成apk 文件
用android studio 打包成apk 文件 js build 执行: react-native bundle --platform android --dev false --entry-fi ...
- 原生js实现删除class和添加class
内容来自百度搜索 //判断样式是否存在 function hasClass(ele, cls) { return ele.className.match(new RegExp("(\ ...
- FCC(ES6写法) Symmetric Difference
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...
- hadoop源码分析(2):Map-Reduce的过程解析
一.客户端 Map-Reduce的过程首先是由客户端提交一个任务开始的. 提交任务主要是通过JobClient.runJob(JobConf)静态函数实现的: public static Runnin ...
- 算法与数据结构(五) 普利姆与克鲁斯卡尔的最小生成树(Swift版)
上篇博客我们聊了图的物理存储结构邻接矩阵和邻接链表,然后在此基础上给出了图的深度优先搜索和广度优先搜索.本篇博客就在上一篇博客的基础上进行延伸,也是关于图的.今天博客中主要介绍两种算法,都是关于最小生 ...