Palindrome Partitioning II Leetcode java
题目:
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.
题解:
这道题需要用动态规划做,如果用I的DFS的方法做会TLE。
首先设置dp变量 cuts[len+1]。cuts[i]表示从第i位置到第len位置(包含,即[i, len])的切割数(第len位置为空)。
初始时,是len-i。比如给的例子aab,cuts[0]=3,就是最坏情况每一个字符都得切割:a|a|b|' '。cuts[1] = 2, 即从i=1位置开始,a|b|' '。
cuts[2] = 1 b|' '。cuts[3]=0,即第len位置,为空字符,不需要切割。
上面的这个cuts数组是用来帮助算最小cuts的。
还需要一个dp二维数组matrixs[i][j]表示字符串[i,j]从第i个位置(包含)到第j个位置(包含) 是否是回文。
如何判断字符串[i,j]是不是回文?
1. matrixs[i+1][j-1]是回文且 s.charAt(i) == s.charAt(j)。
2. i==j(i,j是用一个字符)
3. j=i+1(i,j相邻)且s.charAt(i) == s.charAt(j)
当字符串[i,j]是回文后,说明从第i个位置到字符串第len位置的最小cut数可以被更新了,
那么就是从j+1位置开始到第len位置的最小cut数加上[i,j] cuts[i] = len - i;
|| (s.charAt(i) == s.charAt(j) && matrix[i+1][j-1]))
{
matrix[i][j] = cuts[i] = Math.min(cuts[i], cuts[j+1]+1);
}
}
}
min = cuts[0]-1;
}
Reference:http://blog.csdn.net/ljphhj/article/details/22573983
Palindrome Partitioning II Leetcode java的更多相关文章
- Palindrome Partitioning II Leetcode
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 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 解题笔记
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
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 ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- HDOJ 1301
9852303 2013-12-18 11:47:01 Accepted 1301 0MS 264K 1117 B C++ 泽泽 Jungle Roads Time Limit: 2000/1000 ...
- C++ traits
[本文链接] http://www.cnblogs.com/hellogiser/p/cplusplus-traits.html [分析] 什么是traits?其实它并不是一个新的概念,上个世纪90年 ...
- iOS中的NSTimer 和 Android 中的Timer
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
- php 字符串负值判断
2014年9月9日 11:54:54 $a = '-1'; $b = (int)$a; $c = is_numeric($a); if ($a) { echo 1; //echo 1 } else { ...
- 升级Windows10后Apache服务器启动失败的解决方法
升级windows10系统后,微软内置了ASP.NET的web高级服务,默认安装了IIS服务器和MSSQL数据库,因为80端口被占用的原因,导致Apache服务器无法正常启动,但是MySQL服务一切正 ...
- 快速排序模板qsort(转载)
qsort 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各 ...
- mysql如何利用Navicat 导出和导入数据库
MySql是我们经常用到的数据,无论是开发人员用来练习,还是小型私服游戏服务器,或者是个人软件使用,都十分方便.对于做一些个人辅助软件, 选择mysql数据库是个明智的选择,有一个好的工具更是事半功倍 ...
- 优秀前端工程师应该掌握的内容(转自:github)
程序 标准规范 ECMAScript HTTP 知识储备 作用域/闭包 数据结构 算法 编程范式 函数式 面向对象 基于原型 面向方面 设计模式 软件架构 MVC MVVM 安全 XSS CSRF 富 ...
- swift复合类型
1.元组类型 (tuple) 元组就是多个元素的组合,是一个用圆括号括起来分号分隔的多个数据的一个集合体. 例如:定义一个学生变量,要求姓名 jim,年龄 19,性别 male 的元组变量为 // ...
- cf158B(水题)
题意:1辆出租车可以坐4人,已知k组人每组ki(ki<=4)人去坐车,要求同组人坐同一辆车,求最少需多少辆车.. 4人组的单独算,1人组和3人组一起,如1多余再将1和2匹配即可.... 代码如下 ...