[leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/
题意:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
解题思路:由于这次不需要穷举出所有符合条件的回文分割,而是需要找到一个字符串s回文分割的最少分割次数,分割出来的字符串都是回文字符串。求次数的问题,不需要dfs,用了也会超时,之前的文章说过,求次数要考虑动态规划(dp)。对于程序的说明:p[i][j]表示从字符i到j是否为一个回文字符串。dp[i]表示从第i个字符到最后一个字符,最少的分割次数下,有多少个回文字符串,即分割次数+1。这道题动态规划的思路比较简单,直接上代码吧。
代码:
class Solution:
# @param s, a string
# @return an integer
# @dfs time out
# @dp is how many palindromes in the word
def minCut(self, s):
dp = [0 for i in range(len(s)+1)]
p = [[False for i in range(len(s))] for j in range(len(s))]
for i in range(len(s)+1):
dp[i] = len(s) - i
for i in range(len(s)-1, -1, -1):
for j in range(i, len(s)):
if s[i] == s[j] and (((j - i) < 2) or p[i+1][j-1]):
p[i][j] = True
dp[i] = min(1+dp[j+1], dp[i])
return dp[0]-1
[leetcode]Palindrome Partitioning II @ Python的更多相关文章
- [LeetCode] Palindrome Partitioning II 解题笔记
		
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
 - LeetCode: Palindrome Partitioning II  解题报告
		
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
 - [LeetCode] Palindrome Partitioning II 拆分回文串之二
		
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
 - Leetcode: Palindrome Partitioning II
		
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
 - LeetCode:Palindrome Partitioning,Palindrome Partitioning II
		
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
 - leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
		
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
 - 【leetcode】Palindrome Partitioning II
		
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
 - 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 ...
 
随机推荐
- [代码审计]云ec电商系统代码审计
			
0x00 前言 看了一下博客内最新的文章,竟然是3月28号的,一个多月没写文章了,博客都长草了. 主要是临近毕业,事情繁多,也没有啥时间和心情静下来写.. 不过现在的话,毕业的东西告一段落了,几乎没啥 ...
 - app分组
			
将项目中中的urls.py复制到app当中 清空项目名称文件夹下的urls.py文件中的内容,并写入一下内容 from django.conf.urls import url,include urlp ...
 - Delphi 的链式代码
			
有了一系列的 Helper, Delphi 也可以使用链式代码了. //譬如要把 3.1415926 中的 141 提取为一个整数: var num: Integer; begin num := Pi ...
 - net自定义安装程序快捷方式
			
创建快捷方式对于绝大多数 Windows 用户来说都是小菜一碟了,然而,这项工作却为程序员带来不少麻烦..NET 没有提供简便直接的创建快捷方式的方法,那么在 .NET 中我们如何为应用程序创建快捷方 ...
 - jstat命令 -- Java虚拟机监控统计工具
			
http://blog.sina.com.cn/s/blog_5f5716580100u76r.html 语法:jstat [generalOption | outputOptions vmid [i ...
 - C#写UTF8文件时指定是否含BOM头
			
BOM的基本概念 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE在UCS中是不存在的字符,所以不应该出现在实 ...
 - 在TQ2440上运行perf,生成Flame Graph
			
参考 http://www.cnblogs.com/helloworldtoyou/p/5585152.html http://blog.csdn.net/mtofum/article/detail ...
 - Delphi XE5 Android 运行黑屏卡死的解决方法
			
1. 确保正确安装Android SDK: 开始菜单 > 所有程序 > Embarcadero RAD Studio XE5 > > Android Tools > 打开 ...
 - 将 tomcat 安装成 windows 服务
			
1.下载 tomcat 的windows 压缩包,一般以 .zip ,而且文件名中有 bin 的文件就是 2.解压下载的文件到某一个目录下,eg: TOMCAT_HOME 3.打开 cmd ,运行 % ...
 - iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
			
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...