[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 ...
随机推荐
- 从Windows 2012标准版升级到数据中心版,标准评价版本升级到标准体验版本并激活
对于Windows 7.Windows 8操作系统,可以在图形界面中通过输入序列号,从低版本直接升级到高的版本,例如从Windows 7家庭版升级到专业版或旗舰版.而对于Windows Server ...
- Android如何使用NoHttp
NoHttp 源码及Demo托管在Github欢迎大家Star: https://github.com/yanzhenjie/NoHttp NoHttp是专门做Android网络请求与下载的框架. N ...
- setTimeout
setTimeout(function () { $('#successTip').hide(); location = location; }, 3000);
- 简单来谈谈alloc分配器
之前说道alloc是原G2.9版本的默认的分配器,这篇就把alloc的原理梳理梳理,顺便简单介绍下有关的内存管理. 一般而言,我们通常习惯的内存分配操作和释放操作是这样的: class Foo {…} ...
- Java基础以及与C++的一些对比
这两天回忆一些Java基础,感觉自己很多地方都不是很牢固,也花费在不少时间和不少流量在手机上查资料. 还是写下来这些东西以免再忘记. 同时还是要记住多动手,编程最重要的就是动手敲啊,有想法有疑问就要自 ...
- zabbix通过curl命令判断web服务是否正常并自动重启服务
zabbix通过curl命令判断web服务是否正常并自动重启服务 主要思路: 通过curl命令获取服务器响应码,如果正常返回200,不正常返回000 具体命令: curl -I -s -w " ...
- jQuery Mobile 脚本加载问题
刚开始使用jQuery Mobile,发现很多问题需要重新考虑,比如脚本加载问题. 在普通html中,如果a.html中有链接到b.html,b.html中有类似代码: $(document).rea ...
- RMQ(范围最值问题)算法学习
RMQ算法适合求解对一个数组多次查询给定范围内的最值. 预处理操作: 令d[i,j]表示从i开始,长度为2^j的一段元素的最值,可以用递推公式写出d[i,j] = min{ d[i][j-1], d[ ...
- 在线学习体验大PK 云智慧发布在线教育网站性能监测报告
互联网不但改变了我们的生活.娱乐和消费方式,也推动各行各业进行着快速变革,越来越多的职场人士必须通过不断的学习.充电才能跟上行业发展的步伐,获得职业的提升,而这也引发了国内教育市场的爆炸式发展.据统计 ...
- 【BootStrap】 基础
[BootStrap] 基础 一. 自适应(针对不同设备如手机平板笔电,使页面的宽度适应设备宽度) <meta name="viewport" content="w ...