[LeetCode]题解(python):132-Palindrome Partitioning II
题目来源:
https://leetcode.com/problems/palindrome-partitioning-ii/
题意分析:
给定一个s,可以将s拆成若干个回文子字符串之和,如果拆成了m个子字符串,那么我们称s可以被m-1 cut。那么返回s的最小cut。
题目思路:
这是一个动态规划问题。这里需要二重动态规划,一个用来记录p[i][j]判断s[i][j]是否回文字符串,另外一个ans[i]代表s[:i]的最小cut是多少。如果s[i :j]是回文字符串,那么ans[j] = min(ans[j],ans[i - 1] + 1)。
代码(python):
class Solution(object):
def minCut(self, s):
"""
:type s: str
:rtype: int
"""
size = len(s)
ans = [i for i in range(size)]
p = [[False for i in range(size)] for j in range(size)]
j = 1
while j < size:
i,ans[j] = j - 1,min(ans[j],ans[j - 1] + 1)
p[j][j] = True
while i >= 0:
if s[i] == s[j] and ((j - i) < 2 or p[i+1][j-1]):
p[i][j] = True
if i == 0:
ans[j] = 0
else:
ans[j] = min(ans[j],ans[i - 1] + 1)
i -= 1
j += 1
return ans[size - 1]
[LeetCode]题解(python):132-Palindrome Partitioning II的更多相关文章
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 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 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 ...
- 132. Palindrome Partitioning II
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- 132 Palindrome Partitioning II 分割回文串 II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...
- 动态规划之132 Palindrome Partitioning II
题目链接:https://leetcode-cn.com/problems/palindrome-partitioning-ii/description/ 参考链接:https://blog.csdn ...
随机推荐
- iOS 退出应用程序
退出应用程序,方法很简单,只是动画效果没有那么好. - (void)exitApplication { AppDelegate *app = [UIApplication sharedApplicat ...
- 请问下mtk双卡手机怎样发短信是怎样选择sim卡来发(双卡都可用的情况下)?
如题,我如今可以获取双卡状态,当仅仅有单一卡的时候可以指定sim卡进行发短信,可是双卡都可用的情况下,程序就默认使用卡1发短信了.即使指定了sim卡编号.
- JAVA基础 (二)反射 深入解析反射机制
在谈论到反射这个问题时,你是否有例如以下疑问? 不管是在.NET还是Java中反射的原理和机制是一样的,理解了一种还有一种就能够迎刃而解,想要理解反射首先须要了解底层的一些概念和执行.理解了反射有助于 ...
- Android tp的虚拟按键(virtual key)处理
Android tp的虚拟按键处理 现在在越来越多的Android的手机都是虚拟按键来操作,但是对于开发者来说可能会关心Android对虚拟按键如何处理的.对Linux熟悉的人可能会说,it's ea ...
- 初识Maven
今天开始学习怎样使用maven,听起来挺神奇的东西,我们来一步一步的加以剖析. Maven的一些具体的论文的东西,网上很多博客介绍,这里我就不逐一介绍,下面我们从安装maven开始讲解: (1)Mav ...
- 快照(Snapshot)
一.定义: SNIA(存储网络行业协会)对快照(Snapshot)的定义是:关于指定数据集合的一个完全可用拷贝,该拷贝包括相应数据在某个时间点(拷贝开始的时间点)的映像.快照可以是其所表示的数据的一个 ...
- jQuery Mobile组件
一.页面 jQuery Mobile 应用了 HTML5 标准的特性,在结构化的页面中完整的页面结构分为header.content.footer 这三个主要区域. 在body 中插入内容块: < ...
- English - therefore,so,hence,then,accordingly,thus用法解析
这几个词的区别大致可从以下几方面去看: 1.therefore adv.因此, 所以=for that reason=consequently常用于连接两个并列分句,其前加“and”或分号“:”.He ...
- SQL 语句优化—— (一) 操作符优化
1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用IN的SQL有以 ...
- C#控件大小随窗体大小等比例变化
相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...