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

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

解题思路:

因为是Hard,用上题的结果计算肯定是超时的,本题需要用dp的思路,开一个boolean[][]的数组计算i-j是否为palindrome,递推关系为s.charAt(j) == s.charAt(i) &&  isPal[j + 1][i - 1]) → isPal[j][i] = true,同时dp[i] = Math.min(dp[i], dp[j - 1] + 1),JAVA实现如下:

    public int minCut(String s) {
int[] dp = new int[s.length()];
for (int i = 0; i < dp.length; i++)
dp[i] = i;
boolean isPal[][] = new boolean[s.length()][s.length()];
for (int i = 1; i < s.length(); i++)
for (int j = i; j >= 0; j--)
if (s.charAt(j) == s.charAt(i)
&& (j + 1 >= i - 1 || isPal[j + 1][i - 1])) {
isPal[j][i] = true;
dp[i] = j == 0 ? 0 : Math.min(dp[i], dp[j - 1] + 1);
}
return dp[dp.length - 1];
}

Java for LeetCode 132 Palindrome Partitioning II的更多相关文章

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

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

  2. Leetcode 132. Palindrome Partitioning II

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

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

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

  4. 【LeetCode】132. Palindrome Partitioning II

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

  5. 【leetcode】Palindrome Partitioning II

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

  6. 【leetcode】Palindrome Partitioning II(hard) ☆

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

  7. 132. Palindrome Partitioning II

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

  8. Java for LeetCode 131 Palindrome Partitioning

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

  9. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

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

随机推荐

  1. cordova热更新插件调试

    有更新www目录内容后,首先sencha app build,然后进入 cordova目录 运行 cordova-hcp build, 然后查看 chcp.json文件时间,然后压缩cordova目录 ...

  2. 解决Ubuntu下gedit中文乱码的情况

    windows下简体中文多用GBK编码 (或GB2312, GB18030), 而linux下多用UTF-8编码. 因此,一般来说在微软平台编辑的中文txt不能在ubuntu下直接打开查看,除非在保存 ...

  3. DIY树莓派之随身工具箱

    摆弄树莓派有一年多了,在这里把经验分享给大家,少走弯路. 先放图两张. 搭建目的: wifi信号中转站\网站服务器\IC卡渗透测试\中间人\otr… 基于树莓派3 系统为Kali Linux 2017 ...

  4. 【Java编程】JDBC注入攻击-Statement 与 PreparedStatement

    在上一篇[Java编程]建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement我们介绍了怎样使用JDBC驱动建立一个简单的 ...

  5. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之静态页面

    一.引言 接着上一篇,京东个人中心的所有功能数据分析完成之后,现在需要把静态页面完成,实现过程中要用到的技术有:Bootstrap.html5表单新特性等.除此之外,还要利用Node.js的Expre ...

  6. Ubuntu系统经常使用操作指令说明

    使用U盘拷贝压缩文件 文件的压缩方法详见:3.6文件归档压缩及其释放 U盘直接插入机器USB接口.等待自己主动弹出窗体,在弹出窗体选择"文件->打开终端",打开的终端当前文件 ...

  7. 以其他字段作为某一字段的值. 字段长度char_length(?)

    UPDATE t_dealer a INNER JOIN t_dealer b ON a.id=b.id SET a.zihao=b.shortName where a.zihao is null o ...

  8. java导出excel不须要额外jar包

    眼下我知道的在java中导出Excel能够用poi或在jsp的文件头改变输出流. 以下再介绍一种就用java基础包导出的Excel.导出的格式形如: 源代码例如以下: package csvExcel ...

  9. Nook 2 Root

        最后我还是忍不住root了它,用了差一点够一个月 1.备份2.root 3.装软件=====================================================1. ...

  10. SQLServer待优化语句查询

    SELECT top 100 (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N'总花费时 ...