[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 ...
随机推荐
- spring数据源配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...
- http的响应对象
Servlet 服务器 HTTP 响应 正如前面的章节中讨论的那样,当一个 Web 服务器响应一个 HTTP 请求时,响应通常包括一个状态行.一些响应报头.一个空行和文档.一个典型的响应如下所示: H ...
- #error作用
指令 用途 # 空指令,无任何效果 #include 包含一个源代码文件 #define 定义宏 #undef 取消已定义的宏 #if 如果给定条件为真,则编译下面代码 #ifdef 如果宏已经定义, ...
- 内省(Introspector)
内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...
- C# byte数组转换成List<String>
byte[] bys=buffer; string[] AllDataList= Encoding.Default.GetString(bys).Split(Environment.NewLine. ...
- Razor引擎中的_ViewStart.cshtml
Startup Code是在所有View执行之前加载和执行的代码. 在Razor引擎中的_ViewStart.cshtml 就是装载这些“预执行代码”的文件,它有两个特点: 一.就是所有View执行之 ...
- Android菜鸟成长记4-button点击事件
Button 1.button按钮的创建 一般来说,在我们新建一个Android项目的时候,会有会默认有一个activity_main.xml的文件 如果你在新建项目的时候,把Create Activ ...
- HDOJ 1512 几乎模板的左偏树
题目大意:有n个猴子.每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害.如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认 ...
- iOS7.0适配问题
self.navigationController.navigationBar.translucent = YES,导航栏透明 extendedLayoutIncludesOpaqueBars = Y ...
- u3d shader forge 冰渐冻材质
<ignore_js_op> 分享个自己研究的冰材质渐冻shader可以调节的参数很多,并且带模型顶点偏移,能更加真实模拟冰的凹凸厚度感.(参数过大容易出现模型破损,慎用)shader f ...