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 ...
随机推荐
- c语言:printf系列的函数
/** *----------------------------stdio.h--------------------------------------- * int printf(const c ...
- C#事件快捷设置
注解:本文摘自网络 C# 自定义带自定义参数的事件方法 C# 自定义带自定义参数的事件 需要经过以下几个步骤: 1.自定义事件参数 :要实现自定义参数的事件,首先要自定义事件参数.该参数是个类.继承自 ...
- ICC的sacn-wise和unit-wise
假设有16个被试,2个session,2个RUN,200个ROI,那么ICC需要对RUN1和RUN2分别算两次(相比而言,paired ttest则是对一个session中的RUN1和RUN2计算) ...
- Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例
概要 这一章,我们对TreeMap进行学习.我们先对TreeMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用TreeMap.内容包括:第1部分 TreeMap介绍第2部分 TreeMa ...
- jboss:跟踪所有sql语句及sql参数
默认情况下,hibernate/JPA 在server.log中记录的SQL语句,参数都是用?代替的,这样不太方便. 网上留传的p6spy在最新的jboss上(EAP 6.0+版本)貌似已经不起作用了 ...
- SM2国密证书合法性验证
通常我们遇到过的X509证书都是基于RSA-SHA1算法的,目前国家在大力推行国密算法,未来银行发行的IC卡也都是基于PBOC3.0支持国密算法的,因此我们来学习一下如何验证SM2国密证书的合法性.至 ...
- Infer.net 开源组件: 1, 机器学习入门,要从贝叶斯说起
我的入门方式,先从应用现象中,总结规律反推本质.一头扎进理论书籍是不对的. 老外的先进,还是体现在传承方面.没办法,我们竞争压力大,有好东西藏着掖着.大家都苦逼 我最开始是从介绍,有了基本概念,见xx ...
- 283 Move Zeroes
/** * 题意:将0挪到末尾,并且不改数组中原有元素的顺序 * 解析:找到0元素,然后寻找其后面非0的元素,进行交换位置 * @param {number[]} nums * @return {vo ...
- Android手机截屏
刚开始打算做一个简单的截屏程序时,以为很轻松就能搞定. 在Activity上放一个按钮,点击完成截屏操作,并将数据以图片形式保存在手机中. 动手之前,自然是看书和网上各种查资料.结果发现了解的知识越多 ...
- FileShare枚举的使用(文件读写锁)
开发过程中,我们往往需要大量与文件交互,但往往会出现很多令人措手不及的意外,所以对普通的C#文件操作做了一次总结,问题大部分如下: 1:写入一些内容到某个文件中,在另一个进程/线程/后续操作中要读取文 ...