[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 ...
随机推荐
- 无废话ubuntu 13.4w文件共享配置
目标:实现windows和linux混合组成的操作 系统中可以共享文件,并可以通过机器名互相访问 安装文件共享服务 0.更改本机主机名,修改 /etc/hostname文件和/etc/hosts文件中 ...
- Error pulling origin: error: The following untracked working tree files would be overwritten by...
git在pull时,出现这样的错误的时候,可能非常多人进进行stash.相关stash的请看:Error pulling origin: error: Your local changes to th ...
- Nginx+Tomcat7+Mencached负载均衡集群部署笔记
Nginx+Tomcat+Memcached负载均衡集群服务搭建 操作系统:CentOS6.5 本文档主要解说,怎样在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群s ...
- 编写可维护的JS 03
3.语句和表达式 所有语句都应当使用花括号 if else语句 for 循环 while 循环 do...while try...catch...finally 花括号对齐方式 左括号在第一行语句末尾 ...
- Html页面操作json串
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaSc ...
- springmvc的ModelAndView的简单使用
参考:http://blog.csdn.net/zzjjiandan/article/details/34089313 先上图: MAVTest.java package com.wyl; impor ...
- [LeetCode]题解(python):143-Reorder List
题目来源: https://leetcode.com/problems/reorder-list/ 题意分析: 给定一个链表L:L0→L1→…→Ln-1→Ln,改变链表的排序为: L0→Ln→L1→L ...
- 一个简单的web框架实现
一个简单的web框架实现 #!/usr/bin/env python # -- coding: utf-8 -- __author__ = 'EchoRep' from wsgiref.simple_ ...
- 利用for循环求1-100之间的奇数和 and 0-100的偶数和
为了方便自己计算,以下代码只求1-10的奇数和 and 0-10的偶数和 1-10的奇数从1开始分别为1.3.5.7.9 代码如下 /* Name:循环语句得出奇数.偶数并相加求和 Copyright ...
- win7中注册tomcat服务
非安装版tomcat下载后,在bin文件夹会有一个startup.bat文件,运行该文件即可启动tomcat了.不过在服务器配置tomcat的话,就通常需要注册为服务. 在/bin文件下还有tomca ...