[Leetcode][JAVA] 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.
很有难度的一道题,不看讨论几乎没法accept。
解法看来看去基本就是dp,与一般dp不一样的是,需要对两个特征进行dp记录。
1.使用dp[i]记录s.substring(0,i)的分割数,初始值为dp[i]=i-1. 0<=i<=s.length().
对于每个i, 可以找到至少一个j, 0<=j<=i-1, 使得s.substring(j,i)是回文字符串。找到所有这样的j的集合J。转移函数即为: dp[i] = min(dp[j]+1) (j∈J)
仅仅这么做还是会超时,那么还得继续优化。当前算法的重复处在于,每次判断s.substring(j,i)是否为回文字符串时,都需要遍历这个字符串,所以还需要第二个dp记录s.substring(j,i)是否为回文字符串。
2. 使用isP[i][j]记录s.substring(i,j)是否为回文字符串。0<=i<=s.length(),0<=j<=s.length(). 初始状态isP[i][i]=true (0<=i<=s.length()), isP[i][i+1]=true (0<=i<s.length()).
(PS,后面代码中没有初始化isP[i][i+1],因为在遍历过程中作特殊判断了(i-j<2时必定为真))
这样的话要判断s.substring(i,j)是否为回文字符串,只需要isP[i+1][j-1]为真且s.charAt(i-1)==s.charAt(j).
同时我们也可以明白,遍历的顺序应该是逐渐将i,j距离拉长的。
所以应该有两层循环,第一层循环是用来记录dp[i], i从1到s.length().
第二层循环记录isP[j][i](i已固定), j从i-1到0,反向遍历。
最后dp[s.length()]即为结果
代码如下:
public int minCut(String s) {
int[] dp = new int[s.length()+1];
boolean[][] isP = new boolean[s.length()+1][s.length()+1];
for(int i=0;i<=s.length();i++)
{
isP[i][i]=true;
dp[i]=i-1;
}
for(int i=1;i<=s.length();i++)
for(int j=i-1;j>=0;j--)
if(i-j<2 || (isP[j+1][i-1] && s.charAt(i-1)==s.charAt(j))) {
isP[j][i]=true;
dp[i] = Math.min(dp[i], dp[j]+1);
}
return dp[s.length()];
}
[Leetcode][JAVA] Palindrome Partitioning II的更多相关文章
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- Java for LeetCode 132 Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- mac 使用技巧 (实时更新)
一.部分快捷键. 这个在网上有一大堆,不过笔者认为,还是应该看个人习惯和使用环境吧.如果用得顺手,那就是快捷键,如果用得不顺手,那应该称其为“辅助键”吧. 下面介绍几个常用的快捷键: 1.截图. 屏幕 ...
- 2016-07-15: Window定时器使用
windows下定时器使用实例 #include <iostream> #include <Windows.h> using namespace std; void Timer ...
- 浅谈OpenGL变换矩阵
OpenGL中使用的矩阵全为列向量为主的矩阵. 参考OpenGL变换网站为 http://www.songho.ca/opengl/gl_transform.html 1.什么是GL_MODELVI ...
- boost compile
pushd E:\boost\boost_1_59_0 b2 stage --toolset=msvc-12.0 --without-python --stagedir="E:\boost\ ...
- Python函数中的参数(二)
当使用混合特定的参数匹配模型时,Python将会遵循以下有关顺序的法则: 1.在函数调用中,参数必须以这样的顺序出现:任何位置参数(Value).任何关键字参数(name = Value)和*sequ ...
- MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法
MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法 分类: javaweb2013-06-03 14:4 ...
- CSS盒子模型学习记录2
参考:http://www.blueidea.com/tech/web/2007/4545_2.asp 代码试验: html代码: <!DOCTYPE html PUBLIC "-// ...
- JS 之作用域链和闭包
1.JS无块级作用域 <script> function Main(){ if (1==1){ var name = "alex"; } console.log(nam ...
- DHCP服务器的开始方式
方法一:采用DHCP服务器接口开启的方式 [Huawei]dhcp enable [Huawei]int g0/0/0[Huawei-GigabitEthernet0/0/0]ip add 192.1 ...
- [转]linux /proc/cpuinfo 文件分析
在Linux系统中,提供了proc文件系统显示系统的软硬件信息.如果想了解系统中CPU的提供商和相关配置信息,则可以通过/proc/cpuinfo文件得到.本文章针对该文件进行简单的总结. 基于不同指 ...