leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II
题意:
给定一个字符串s,分区使分区的每个子字符串都是回文。
返回对于s的回文分割所需的最小削减。
例如,给定s =“aab”,
返回1,因为可以使用1切割生成回文分割[“aa”,“b”]。
思路:
一开始我用dfs + 贪心来做,找到最长的回文,然后拆掉继续dfs,然后最后一个测试样例过不了,不是复杂度的问题,是算法本身有问题。
看了别人的思路,是用dp来做的,设DP[i][j]表示第i个字符到第j个字符是否构成回文,若是,则DP[i][j]=1;若否,则DP[i][j]=0;如此,根据回文的约束条件(对称性),DP[i][j]构成回文需满足:
1、输入字符串s[i]==s[j],对称性;
2、条件1满足并不能保证i到j构成回文,还须:(j-i)<=1或者DP[i+1][j-1]=1;即,i、j相邻或者i=j,也就是相邻字符相等构成回文或者字符自身构成回文,如果i、j不相邻或者相等,i到j构成回文的前提就是DP[i+1][j-1]=true
所以状态转移方程:DP[i][j]=(s[i]s[j]&&(j-i<=1||DP[i+1][j-1]1))。由于i必须小于j,i>=0&&i<s.length().如此,DP[i][j]构成一个下三角矩阵,空间、时间复杂度均为O(n2),如下所示:对于长度为4的字符串s=“baab”,其中红色部分为i>j,为无意义部分;绿色部分i==j,即字符串自身必然构成回文,DP[i][j]=1;白色部分,为长度大于1的子串,需要状态转移方程进行判断。
ac代码:
C++
class Solution {
public:
int minCut(string s) {
int len = s.length();
vector<vector<bool> >map(len,vector<bool>(len));
for(int i = len - 1; i >= 0; i--)
{
for(int j = i; j < len; j++)
{
if(i == j)
{
map[i][j] = true;
}
else if(i + 1 == j)
{
if(s[i] == s[j])
{
map[i][j] = true;
}
}
else
{
if(s[i] == s[j] && map[i + 1][j - 1])
{
map[i][j] = true;
}
}
}
}
vector<int> res(len + 1,0);
for(int i = len - 1; i >= 0; i--)
{
res[i] = res[i + 1] + 1;
for(int j = i + 1; j < len; j++)
{
if(map[i][j] == true && res[j + 1] + 1 < res[i])
res[i] = res[j + 1] + 1;
}
}
return res[0] - 1;
}
};
python
参考博文
leetcode132. Palindrome Partitioning II的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 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 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- ubuntu14.04安装使用NviDIA显卡驱动
想给自己的ubuntu换N卡驱动的原因: 一方面,由于自己电脑在编译源代码8线程全开(make -j8)时,CPU温度呼呼涨到八九十度,从而常常导致系统保护自动关机,让人有点不爽.网上有说ubuntu ...
- 20180830 安装git时报错,
安装:https://blog.csdn.net/u013256816/article/details/54743470 解决问题:https://blog.csdn.net/daojibruce/a ...
- <转>MYSQL数据库数据拆分之分库分表总结
数据存储演进思路一:单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 数据存储演进思路二:单库多表 随着用户数量的 ...
- tomcat已启动,使用maven的deploy发布后,根据路径打开浏览器访问时报错HTTP Status 500 - Error instantiating servlet class
web项目中请求出现错误,如下: HTTP Status 500 - Error instantiating servlet class XXXX类 type Exception report mes ...
- 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)
问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器
- scala学习7--class、object、trait
scala语言中没有static成员存在,但是scala允许以某种方式去使用static成员这个就是伴生机制,所谓伴生,就是在语言层面上,把static成员和非static成员用不同的表达方式,cla ...
- jmeter------reponse报错”Unknown column 'XXXXX' in 'where clause'“
一.问题描述 jmeter添加了与数据库mysql的连接,编写完JDBC Request之后,运行提示报错”Unknown column 'be7f5b6e750bb6becf855386338644 ...
- poj1040 Transportation(DFS)
题目链接 http://poj.org/problem?id=1040 题意 城市A,B之间有m+1个火车站,第一站A站的编号为0,最后一站B站的编号为m,火车最多可以乘坐n人.火车票的票价为票上终点 ...
- .NET基本权限系统框架源代码
DEMO下载地址: 百度网盘:http://pan.baidu.com/s/147ilj http://download.csdn.net/detail/shecixiong/5372895 一.开发 ...
- Http 请求 GET和POST的区别
GET和POST还有一个重大区别,简单的说: GET产生一个TCP数据包;POST产生两个TCP数据包. 长的说: 对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器 ...