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 ...
随机推荐
- ajax异步举例
SelectInfo = { release_url: "/compatible/getReleaseFor", project_url: "/compatible/ge ...
- 使用 JavaScript File API 实现文件上传
概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情.虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScr ...
- js抽奖跑马灯程序
js抽奖跑马灯程序 点击下载代码
- java多线程系类:JUC原子类:02之AtomicLog原子类
概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...
- h1/title,b/strong,i/em 的区别
< strong > 表示html页面上的强调(emphasized text), < em > 表示句子中的强调(即强调语义) 1.b和strong的区别 盲人朋友使用阅读设 ...
- typeof关键字简介 -rtti
typeof关键字是C语言中的一个新扩展.只要可以接受typedef名称,Sun Studio C 编译器就可以接受带有typeof的结构,包括以下语法类别: 声明 函数声明符中的参数类型链表和返回类 ...
- Spring IOC 源码浅析
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...
- Theano2.1.1-基础知识之准备工作
来源:http://deeplearning.net/software/theano/tutorial/index.html#tutorial 这里介绍的是使用theano的一些基础知识,虽然thea ...
- 完全开源Android网络框架 — 基于JAVA原生的HTTP框架
HttpNet网络请求框架基于HttpUrlConnection,采用Client + Request + Call的请求模型,支持https默认证书,数字安全证书.支持http代理!后续将会实现队列 ...
- 新时代的coder如何成为专业程序员
在移动互联网"泛滥"的今天,越来越多非专业(这里的非专业指的是非计算机专业毕业的程序员)程序员加入到了IT行业中来了,可能是因为移动互联网的火爆导致程序员容易就业而且工资很高,可能 ...