求次数的问题一般用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的更多相关文章

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

  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. [Leetcode][JAVA] Palindrome Partitioning II

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

  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. Win7安装Redis

    首先, 到 https://github.com/MSOpenTech/redis/releases 下载Redis的windows 64bit port zip 解压后放到某个目录下, 例如 c:\ ...

  2. php进阶函数

    1,对文件的操作,确保多个进程可以同时读写一个文件(flock函数) flock($hamdle,int $operator) operator的取值,LOCK_SH(共享锁定,读取程序),LOCK_ ...

  3. 【点滴积累,厚积薄发】修改hosts,并刷新dns缓存

    Windows系统下hosts位置 C:\Windows\System32\drivers\etc 修改hosts后,要想马上生效,需要运行命令来刷新DNS缓存:ipconfig /flushdns

  4. 通用权限管理系统数据字典 V3.9 版本,欢迎开发个各种业务系统的朋友们,参考表结构

    C#.NET通用权限管理系统组件数据字典 导  航 ◇ (01) BaseItemDetails ◇ (02) BaseItems ◇ (03) BaseLog ◇ (04) BaseMessage ...

  5. jboss EAP 6.2+ 通过代码控制JNDI数据源

    通过Jboss提供的API,可以操控JBoss,效果跟在管理控制台手动操作完全一样,下面是示例代码: 一.pom.xml添加依赖项 <dependency> <groupId> ...

  6. jboss eap 6.3 域(Domain)模式配置

    jboss提供了二种运行模式:standalone(独立运行模式).domain(域模式),日常开发中,使用standalone模式足已:但生产部署时,一个app,往往是部署在jboss集群环境中的, ...

  7. 【转】加快网站访问速度——Yslow极限优化

    Yslow是一套雅虎的网页评分系统,详细的列出了各项影响网页载入速度的参数,这里不做多说. 我之前就一直参考Yslow做博客优化,经过长时间的学习也算是有所收获,小博的YslowV2分数达到了94分( ...

  8. 解决Kafka-1194问题

    生产环境中使用Kafka作为日志处理的中间件,系统结构是这样的.自12月上线一个多月来,系统运行稳定. 用过kafka的都知道,Kafka产生的消息全部存储到硬盘文件中,并且在消息被消费后不会被立即删 ...

  9. 无法将分支 master 发布到远程 origin,因为远程存储库中已存在具有同一名称的分支

    无法将分支 master 发布到远程 origin,因为远程存储库中已存在具有同一名称的分支.发布此分支将导致远程存储库中的分支发生非快进更新. 第一次用oschina的git设置完远程仓库后提交出现 ...

  10. AlertDialog之常见对话框(单选对话框、多选对话框、进度条对话框)

    单选对话框,顾名思义就是只能选一项(setSingleChoiceItems(Items,)) public void click(View v){ //创建对话框类 AlertDialog.Buil ...