动态规划之132 Palindrome Partitioning II
题目链接:https://leetcode-cn.com/problems/palindrome-partitioning-ii/description/
参考链接:https://blog.csdn.net/jingsuwen1/article/details/51934277
dp[i]存放[0,i)即以前i个字符串的子串的最小切割数,则所求为dp[s.length()];
前0个字符串和1个字符串不需要切割,所以dp[0]=0,dp[1]=0;
1.初始化:当字串s.substring(0,i)([0,i)个字符)是回文时,dp[i] = 0(表示不需要分割);否则,dp[i] = i-1(表示至多分割i-1次);
比如abc最多切割2次(a|b|c),aa不需要切割
2.对于任意大于1的i,如果s.substring(j,i)( 1 =< j <= i ,即遍历i之前的每个子串)是回文时,dp[i] = min(dp[i], dp[j]+1);
(注:j不用取0是因为若j == 0,则又表示判断(0,i))。
public int minCut(String s) {
if(s == null||s.length() == 0)
return 0;
int[] dp=new int[s.length()+1];
dp[0]=0;//空字符,不需要切割
dp[1]=0;
for (int i = 2; i < dp.length; i++) {
dp[i]=is_palindrome(s.substring(0,i))?0:i-1;
}
for(int i=2;i<=s.length();i++)
{
// 1=<j<=i的子串回文判定
for(int j=i;j>=1;j--)
{
if(is_palindrome(s.substring(j,i)))
{
dp[i]=Math.min(dp[i],dp[j]+1);
}
}
}
return dp[s.length()];
}
//判断回文串例程
public boolean is_palindrome(String s)
{
//System.out.println(s);
StringBuilder sb=new StringBuilder(s);
return s.equals(sb.reverse().toString());
}
上述的dp方法在leetcdoe上显示超时,在牛客网上可以通过。所以期待更加完全的dp方法。
动态规划之132 Palindrome Partitioning II的更多相关文章
- 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 ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 132. Palindrome Partitioning II
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 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
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 132 Palindrome Partitioning II 分割回文串 II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...
随机推荐
- cocos2d-x c++ (多种屏幕Android与iOS的适配原理)
1.AppDelegate.cpp 文件中 bool AppDelegate::applicationDidFinishLaunching() { // initialize director aut ...
- java多线程小题一瞥
有如下线程类定义: public class MyThread extends Thread { private static int num = 0; public MyThread() { num ...
- pytorch进行图像分类的流程,下一篇为实例源代码解析
一.预处理部分 1.拿到数据首先对数据进行分析 对数据的分布有一个大致的了解,可以用画图函数查看所有类的分布情况.可以采取删除不合理类的方法来提高准确率: 对图像进行分析,在自定义的图像增强的多种方式 ...
- 第二章:Opencv核心類Mat
Opecv就是做計算機視覺,就是讲图片转换成计算机所能识别的数据 Mat类中由大量的内联函数,主要就是用于提高速度. 一般类型都用rgb,存的时候用CV_8UC3.create函数一般会把原来的空间释 ...
- Ubuntu中使用pip3报错
使用pip3 出现以下错误: Traceback (most recent call last): File “/usr/bin/pip3”, line 9, in from pip import m ...
- DataGridView控件用法二:常用属性
通常会设置的DataGridView的属性如下: AllowUserToAddRows - False指示是否向用户显示用于添加行的选项,列标题下面的一行空行将消失.一般让其消失.AllowUserT ...
- JavaScript-合同到期续约案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- EF性能优化
下面总结了一些在使用EF的过程中应当特别注意的地方,避免大家再走弯路. 1.分清真分页和假分页 大家都知道分页分为真分页和假分页,并且假分页是特别耗费性能的.我们在使用的过程中也是以真分页为主,但是在 ...
- JS实例4
根据当前年的前五年后五年的年月日 <select id="nian" onclick="Bian()"></select>年 <s ...
- leetCodelinked-list-cycle-ii找到链表的环
题目 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. N ...