Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP
class Solution(object):
def minCut(self, s):
"""
:type s: str
:rtype: int
"""
n = len(s)
maxInt = 2147483647
cuts = [maxInt for x in range(n)]
p = self.palinTable(s)
for i in range(n):
temp = maxInt
if p[0][i] == True:
cuts[i] = 0
else:
for j in range(i):
if p[j+1][i] and temp > cuts[j] + 1:
temp = cuts[j] + 1
cuts[i] = temp
return cuts[-1] def palinTable(self, s):
n = len(s) p = [[False for x in range(n)] for y in range(n)] for i in range(n):
p[i][i] = True for i in range(n-1):
if s[i] == s[i+1]:
p[i][i+1] = True for curLen in range(3,n+1):
for i in range(n-curLen+1):
j = i + curLen-1
if s[i] == s[j] and p[i+1][j-1]:
p[i][j] = True return p
Leetcode 132. Palindrome Partitioning II的更多相关文章
- leetcode 132. Palindrome Partitioning II ----- java
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 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】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
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 ...
- [Leetcode][JAVA] 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_ Hard tag: Dynamic Programming
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- xhEditor用法
xhEditor是一个基于jQuery开发的简单迷你并且高效的在线可视化HTML编辑器,而且兼容很多浏览器,所以就选它了,具体使用如下: 1 .下载xhEditor 最新版本 下载地址:http:// ...
- SharePoint 2013无代码实现列表视图的时间段动态筛选
本文介绍两种为列表视图设置时间段筛选器的方法.其中,第一个方法用于SharePoint Server,第二个方法同时还能用于SharePoint Foundation. 方法一:日期筛选器Web部件 ...
- HBase简介
HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HB ...
- jQuery使用.on()无法绑定hover
发现好像没有hover这个事件,jQuery的hover事件是一个封装,hover算不得一个事件.他只是将mouseover和mouseout合并了用mouseover和mouseout两个配合效果好 ...
- Eclipse调试常用技巧(转)
Eclipse调试常用技巧 转自http://daimojingdeyu.iteye.com/blog/633824 1. 条件断点 断点大家都比较熟悉,在Eclipse Java 编辑区的行头双击就 ...
- 微信公众平台开发(71)OAuth2.0网页授权
微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...
- 一个看似很简单的SQL却难倒了很多人
一个选课表,有学生id,课程id,老师id,要求选出同时选了语文和数学的学生 USE [tempschool] GO /****** 对象: Table [dbo].[SelectC] 脚本日期: 0 ...
- Mecanim动画模型规范
面数控制, 以三角面计算 不要超过4边的面 光滑组,法线 单位CM,单位比例 中心点 3DMax:Reset Transform Maya:Freeze Transformation 帧率:30帧 不 ...
- 转一篇Unity客户端与Java服务器的通信
转自:http://www.programering.com/a/MTNxYDMwATQ.html A few days ago a friend asked me about Unity3D ins ...
- 配置JAVA环境变量
1.安装JDK包. 2.安装完成后,[开始]-[运行]输入"cmd","java -version",如果正确输出,表示安装成功. 3.右键[我的电脑]-[属性 ...