Palindrome Partitioning II

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.

标签: Dynamic Programming

分析:动态规划,设f[i,j]表示区间[i,j]的最少cut数,所以状态方程为:

      f[i,j]=min(f[i,k],f[k+1,j])   (i<=k<=j);

在二维dp的基础上在优化成一维dp:

设f[i]表示i到len-1的最少cut数,所以状态方程为;

      f[i]=min(f[i],f[j+1]+1)   (s[i...j]为回文字符串&&i<=j<<len-1)

判断s[i...j]是否为回文字符串也可以用动态规划,可以新建boolean数组isPal[len][len],isPal[i][j]表示s[i][j]为回文字符串;

参考代码:

 public class Solution {
public int minCut(String s) {
int len=s.length();
int cutnum[]=new int[len+1];
boolean isPal[][]=new boolean[len][len];
for(int i=0;i<=len;i++){
cutnum[i]=len-1-i;//先假设i到len-1间每个字符都cut
}
for(int i=len-1;i>=0;i--){
for(int j=i;j<len;j++){
if(s.charAt(i)==s.charAt(j)&&(j-i<2||isPal[i+1][j-1])){
isPal[i][j]=true;
cutnum[i]=Math.min(cutnum[i], cutnum[j+1]+1);
}
}
}
return cutnum[0];
}
}

LeetCode-Palindrome Partitioning II[dp]的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 解题笔记

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

  2. LeetCode: Palindrome Partitioning II 解题报告

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

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  4. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  5. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  6. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  7. 【leetcode】Palindrome Partitioning II

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

  8. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

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

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

  10. 【LeetCode】132. Palindrome Partitioning II

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

随机推荐

  1. JS语句

    JS语句包括: 1.顺序语句 2.分支语句:  if...else                   switch...case 3.循环语句 一.先看顺序语句: </body> < ...

  2. 浅谈OpenStack架构

    首先,先来了解,什么是OpenStack?   OpenStack是一个云平台管理的项目,它不是一个软件.这个项目由几个主要的组件组合起来完成一些具体的工作.OpenStack是一个旨在为公共及私有云 ...

  3. 移动端响应式布局+rem+calc()

    1.媒体查询:@media only screen and (max-width: ) {},在最初做pc端时,使用各种媒体查询,因为pc的屏幕分辨率总共就几种,不嫌麻烦的重复使用类名.有很大的缺陷就 ...

  4. Xmanager连接到RedHat Enterprise Linux 6.8

    RedHat Enterprise Linux 6 配置Xmanager ,实现图形界面连接 X是用在大多数UNIX系统中的图形支持系统.如果你在你的Linux机器上使用GNOME或者KDE的话,你就 ...

  5. php打包文件为ZIP包后下载到本地

    这是一个工作中需要打包下载当前产品的所有图片到本地,文件格式为ZIP压缩包,打包下载文件跟图片一样,本程序细节为实际情况,使用需按照自己实际情况书写:<?php/**************** ...

  6. java怎么发http请求

    package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  7. 遇到attemp to invoke virtual method

    这个很大原因是没有预先初始化sdk,检查application的配置是否配置了application:name

  8. c# webbrower 代理 类 IEProxy

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...

  9. Java jvm级别native关键词、JNI详解

    1.native关键词的引入 再完美的编程语言也有自己的不足之处,当然Java也不例外,Java的不足之处除了体现在运行速度(这点往往被一些其他编程语言使用者所诟病)上要比传统的C++慢许多之外,Ja ...

  10. (转载)Sybase:bcp命令参考

    参考文档: http://blog.csdn.net/wwp1026/article/details/6900569