题目链接: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的更多相关文章

  1. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  2. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  3. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. 132. Palindrome Partitioning II

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  5. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 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 ...

  7. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

  8. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  9. 132 Palindrome Partitioning II 分割回文串 II

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...

随机推荐

  1. 迭代器模式(java版)

    迭代器模式的组成部分 Aggregate(抽象聚合类) 它用于存储和管理元素对象,声明一个createiterator()方法用于创建一个迭代器对象,充当抽象迭代器工厂角色. ConcreteAggr ...

  2. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

  3. javaweb之验证码验证技术

    今天学习了一个验证码校验技术,所以就写下了一些笔记,方便日后查看.首先创建web工程 1.然后在src目录下创建一个Servlet类,此类用来显示登录页面和错误信息提示 package com.LHB ...

  4. 第一章 HTML基本标签

    1.HTML:HTML:超文本标签语言(标签又称标记.元素).浏览器:“解释和执行”HTML源码的工具 (运行网页的工具APP).客户端:享受服务的计算机服务器:提供服务的计算机 2.基本框架(网页最 ...

  5. js控制元素隐藏和显示

    原生: 方法一: document.getElementById("idname").style.visibility="hidden"; document.g ...

  6. websocket发送接收协议

    一.websocket接收数据 1)固定字节(1000 0001或1000 0010);   ---区分是否是数据包的一个固定字节(占1个字节) 个字节是数据的长度; 3)mark 掩码为包长之后的 ...

  7. websocket服务器握手协议

    测试网页代码如下 <!DOCTYPE html> <html> <head> <title>测试 websocket 世界最简单案例</title ...

  8. grouping sets 的使用

    grouping sets 可以用于在计算分组聚合函数值的同时计算合计值 -- 查询结果group_id为NULL的那一行即为合计值 select group_id , count(distinct ...

  9. DOM jquery

    DOM  文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM ...

  10. 关于数据库主从表、主键PRIMARY KEY 外键约束 FOREIGN KEY 约束----NOT NULL,DEFAULT,CHECK

    如果由两个列共同组成主键,而且一个子表将主键作为可为空值的外键来继承,就可能得到错误的数据.可在一个外键列中插入有效的值,但在另一个外键列中插入空值.然后,可添加一个数据表检查约束,在可为空的外键中检 ...