动态规划之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 分 ...
随机推荐
- Javascript-do_while....
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Mongodb 文档时间字段修改
mongo文档[tblEvent]如下: { "_id" : ObjectId("5a0415f9bf28b684b1c7f5b2"), &qu ...
- linux下安装mysql等信息
1.安装 apt-get update;// 第一次的时候,你更新一下你的软件包的源地址数据; apt-get install mysql-server 2.账号登陆 mysql -h localho ...
- <8>Cocos Creator组件开发cc.Component
1.组件简介 组件是Cocos Creator的主要构成,渲染(场景显示内容).逻辑.用户输入反馈.计时器等等几个方面都是由组件完成的.根据Cocos Creator的总体架构,组件和节点配合完成游戏 ...
- spriteJS
https://blog.csdn.net/qq_37261367/article/details/84662028
- android本地数据库,微信数据库WCDB for Android 使用实例
android本地数据库,微信数据库WCDB for Android 使用实例 Home · Tencent/wcdb Wikihttps://github.com/Tencent/wcdb/wiki ...
- css的优先级 和 权重
之前写页面样式时,有时会遇到 用多条样式定义规则对同一个元素进行样式设置的时候,当时想到的就是 按css选择器的优先级来搞定这个问题,说实话当时也就只记得 内嵌样式 > id > cla ...
- 2017年3月29日 webService入门理解 二
前边说到了N多webService的概念. 其实,说白了,我个人理解的话,webService就是一个“概念”.就好像互联网一样,就是一个很虚幻,很高的一个概念.同样,webService也是.互联网 ...
- Git HEAD detached from XXX (git HEAD 游离) 解决办法
本文 Git 图片主要来自:图解 Git,非常感谢! 读完本文你将了解: 什么是 HEAD HEAD 游离状态的利与弊 具体解决操作 Thanks 什么是 HEAD Git 中的 HEAD 可以理解为 ...
- Python基础教程之udp和tcp协议介绍
Python基础教程之udp和tcp协议介绍 UDP介绍 UDP --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议.UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但 ...