动态规划——Palindrome Partitioning II
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
dp[i] = min(dp[j-1]+1),当0<j<=i时,并且s[j..i]是回文时
dp[i] = 0 ,当j=0,并且s[j..i]是回文时
在具体的dp数组递推运算过程中,需要这两个分支,同时会更新pa[j][i]的值便于后面的过程中回文条件的判断。
public int minCut(String s) {
int slen = s.length();
int[][]pa = new int[slen][slen];
int[]dp = new int[slen];
for(int i = 0;i<slen;i++)
dp[i] = i;
for(int i = 0;i<slen;i++)
Arrays.fill(pa[i],0);
for(int i = 1;i<slen;i++) {
for(int j = 0;j<=i;j++) {
if(s.charAt(j)==s.charAt(i)&&((i-j<2)||pa[j+1][i-1]==1)) {
pa[j][i] = 1;
if(j!=0)dp[i] = Math.min(dp[i],dp[j-1]+1);
else dp[i] = 0;
}
}
}
return dp[slen-1];
}
动态规划——Palindrome Partitioning II的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 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 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
随机推荐
- ASP.Net获取Aras连接,并获取Innovator实例
首先需要在自己的项目bin目录下引入Aras的dll(../Aras\Innovator\Innovator\Server\bin). 注意:在引入Aras的dll时.需要注意自己的操作系统的位数.因 ...
- lillietest 正态分布的拟合优度测试
函数 lillietest格式 H = lillietest(X) %对输入向量X进行Lilliefors测试,显著性水平为0.05.H = lillietest(X,alpha) %在水平alpha ...
- .net 解压缩 rar文件
public static class RARHelper { public static bool ExistsWinRar() { bool result = false; string key ...
- Microsoft Internet Explorer v11 / XML External EntityInjection 0day
[+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org[+] Source: http://hyp3 ...
- DES加密ECB(模式) golang
Java默认DES算法使用DES/ECB/PKCS5Padding,而golang认为这种方式是不安全的,所以故意没有提供这种加密方式,那如果我们还是要用到怎么办?下面贴上golang版的DES EC ...
- 自适应rem.js
用rem.js来实现自适应屏幕大小,要注意border不用rem做单位 代码如下: (function (doc, win) { //orientationchange : 判断手机是水平方向还是垂 ...
- Spring缓存注解@Cacheable
@Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓存的名称, ...
- 关于ViewPager+Fragment中的坑
1.我的情况是Activity里嵌套了Fragment_0,然后Fragment_0里面又嵌套了两个Fragment:Fragment_1.Fragment_2,然后我在其中一个Fragment,Fr ...
- day17——其他内置函数
zip函数: print(list(zip(('a','b','c'),(1,2,3)))) p={'name':'alex','age':18,'gender':'none'} print(list ...
- Linux库多重依赖
源文件: //world.cpp #include <stdio.h> void world(void) { printf("world.\n"); } //hello ...