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 ...
随机推荐
- python OS 模块 文件目录操作
Python OS 模块 文件目录操作 os模块中包含了一系列文件操作的函数,这里介绍的是一些在Linux平台上应用的文件操作函数.由于Linux是C写的,低层的libc库和系统调用的接口都是C AP ...
- SSL证书生成方法【转】
转自 SSL证书生成方法 - fyang的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/fyang2007/article/details/6180361 一般情况 ...
- Elasticsearch5.0 安装问题集锦【转】
转自 Elasticsearch5.0 安装问题集锦 - 代码&优雅着&生活 - 博客园http://www.cnblogs.com/sloveling/p/elasticsearch ...
- videojs做直播、弹幕
从上一年开始,我们开始接触直播,现在直播成本真的很低,很多CDN供应商都有提供,本文只是大概讲述播放器这个话题. 开始调研 播放格式,我挑了三种.分别是HLS,RTMP,HTTP-FLV. 下面简单说 ...
- Windows下Oracle数据库自动备份批处理脚本
expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...
- jenkins+docker持续集成实验
在互联网时代,对于每一家公司,软件开发和发布的重要性不言而喻,目前已经形成一套标准的流程,最重要的组成部分就是持续集成(CI)及持续部署.交付(CD).本文基于Jenkins+Docker+Git实现 ...
- HTTP协议头注射漏洞实例
HTTP 响应头文件中包含未经验证的数据会引发 cache-poisoning.cross-site scripting.cross-user defacement.page hijacking.co ...
- NoSql数据库 设计上面的一些心得
NoSql数据库这个概念听闻许久了,也陆续看到很多公司和产品都在使用,优缺点似乎都被分析的清清楚楚.但我心里一直存有一个疑惑,它的出现究竟是为了解决什么问题? 用户信息表,书籍信息表,用户为书籍打分信 ...
- HashMap碰撞问题
HashMap是最常用的集合类框架之一,它实现了Map接口,所以存储的元素也是键值对映射的结构,并允许使用null值和null键,其内元素是无序的,如果要保证有序,可以使用LinkedHashMap. ...
- Nodejs JSON.parse()无法解析ObjectID和ISODate的问题
一个早上搞清楚了一个问题,关于Nodjes JSON.parse()方法只能解析字符串.布尔值.数字等,但不能解析ObjectID及ISODate的值 原因:<How to handle Obj ...