题目来源:

  https://leetcode.com/problems/palindrome-partitioning-ii/


题意分析:

  给定一个s,可以将s拆成若干个回文子字符串之和,如果拆成了m个子字符串,那么我们称s可以被m-1 cut。那么返回s的最小cut。


题目思路:

  这是一个动态规划问题。这里需要二重动态规划,一个用来记录p[i][j]判断s[i][j]是否回文字符串,另外一个ans[i]代表s[:i]的最小cut是多少。如果s[i :j]是回文字符串,那么ans[j] = min(ans[j],ans[i - 1] + 1)。


代码(python):

 class Solution(object):
def minCut(self, s):
"""
:type s: str
:rtype: int
"""
size = len(s)
ans = [i for i in range(size)]
p = [[False for i in range(size)] for j in range(size)]
j = 1
while j < size:
i,ans[j] = j - 1,min(ans[j],ans[j - 1] + 1)
p[j][j] = True
while i >= 0:
if s[i] == s[j] and ((j - i) < 2 or p[i+1][j-1]):
p[i][j] = True
if i == 0:
ans[j] = 0
else:
ans[j] = min(ans[j],ans[i - 1] + 1)
i -= 1
j += 1
return ans[size - 1]

[LeetCode]题解(python):132-Palindrome Partitioning II的更多相关文章

  1. 【leetcode dp】132. Palindrome Partitioning II

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

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

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

  3. 【LeetCode】132. Palindrome Partitioning II

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

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

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

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

  6. 132. Palindrome Partitioning II

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

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

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

  8. Leetcode 132. Palindrome Partitioning II

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

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

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

  10. 动态规划之132 Palindrome Partitioning II

    题目链接:https://leetcode-cn.com/problems/palindrome-partitioning-ii/description/ 参考链接:https://blog.csdn ...

随机推荐

  1. javascript 的加载方式

    本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading),延迟加载(lazy loading),延迟执行(lazy execution),async 属 ...

  2. 转载:JS触发服务器控件的单击事件

    原文地址:http://blog.csdn.net/joyhen/article/details/8485321 <script src="../Js/jquery-1.4.2.min ...

  3. SqlDataAdapter.Update批量数据更新

    SqlDataAdapter.Update批量数据更新 使用SqlDataAdapter.Update可以方便地对数据库进行快速.批量数据更新.我们最常用的多条数据更新方法是使用循环多次执行SQL语句 ...

  4. HDFS之HBase伪分布安装

    1.HBase简介 HBase是Apache Hadoop中的一个子项目,Hbase依托于Hadoop的HDFS作为最基本存储基础单元,通过使用hadoop的DFS工具就可以看到这些这些数据 存储文件 ...

  5. jquery的$().each,$.each

    在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...

  6. ListView 条目加载上滑下滑首尾缩放动画实现

    要实现这个效果,只需要再适配器getView之前,给每个条目的view设置相应的动画即可. 首先需要2个动画的xml文件. 在res下新建anim文件夹:(res/anim) 第一个动画xml文件: ...

  7. Javascript 设计模式笔记

    设计模式太多了 还有些模式概念非常接近(比如观察者 中介者 和 事件发布/订阅模式) 构造器模式 var newObject = {} var newObject = new XXX(); 模块模式 ...

  8. Vue.js——webpack

    Vue.js——60分钟webpack项目模板快速入门 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开 ...

  9. mysql alter table

    准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...

  10. 转: mysql create view 创建视图

    以下的文章主要是对MySQL视图的描述,其中包括MySQ视图L概述,以及创建MySQL视图-create view与修改MySQL视图--alter view等相关内容的具体描述,以下就是文章的具体内 ...