动态规划之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 分 ...
随机推荐
- 比较两种方式的form请求提交
[一]浏览器form表单提交 表单提交, 适用于浏览器提交.像常见的pc端的网银支付,用户在商户商城购买商品,支付时商家系统把交易数据通过form表单提交到三方支付网关,然后用户在三方网关页面完成支付 ...
- jQuery-手风琴伸缩效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- input 滑块功能range javascript方法使用
<script> var rangelist=document.querySelectorAll('[type="range"]'); for(var i=0; i&l ...
- 使用spring的特殊bean完成分散配置
1.在使用分散配置时,spring的配置文件applicationContext.xml中写法如下: <!-- 引入db.properties文件, --> <context:pro ...
- C# Control.Invoke匿名委托
if (txbValue.InvokeRequired) txbValue.Invoke(new MethodInvoker(delegate() { ...
- SQL 跟据出生日期求年龄
最近做项目时遇到一个问题. 跟据人员的生日与当前日期进行比较求出该人员实际年龄.这个看上去比较简单的问题,其实不细心去看也会有很多问题. 先看第一种: 一张人员信息表里有一人生日(Birthday)列 ...
- uvm设计分析——field automation
uvm中的field_automation主要实现了class中的基础元素的copy,compare等函数, 实现方式分为两种:1)用户注册,field系列宏:uvm内部调用static status ...
- Visual Studio快捷键查询
Ctrl+E,D —-格式化全部代码 Ctrl+E,F —-格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- PHP运行脚本
PHP运行脚本 php.exe -f "php文件" php.exe -r "php代码" 例如:在cmd中 C:\Users\Administrator.SK ...
- git server 配置
因为后面要采用Git代替Subversion,花了点时间配置了Git服务端和客户端,像以前一样,仍然基于最新的Ubuntu11.10 server/desktop系统. 感谢这几篇文章的作者: htt ...