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 ...
随机推荐
- 在Oracle中查询表的大小
SELECT segment_name AS TABLENAME,round(BYTES/1024/1024,2) FROM user_segments WHERE segment_name='表名 ...
- Mysql储存过程1: 设置结束符与储存过程创建
#显示储存过程 show procedure status; #设置结束符 delimiter $; #创建储存过程 create procedure procedure_name() begin - ...
- 41、和为S的连续正数序列
一.题目 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- cin循环输入控制问题
之前写一个简单的输入节点值自动生成链表的测试程序,发现cin的输入控制好像在VC++6.0和VS2010中不一样,特此记录. 现在有以下代码: vector<int> ivec; int ...
- Machine Learning系列--TF-IDF模型的概率解释
信息检索概述 信息检索是当前应用十分广泛的一种技术,论文检索.搜索引擎都属于信息检索的范畴.通常,人们把信息检索问题抽象为:在文档集合D上,对于由关键词w[1] ... w[k]组成的查询串q,返回一 ...
- MySQL 约束类型
约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性. MYSQL中,常用的几种约束: 约束类型: 主键 外键 唯一 非空 自增 默认值 关键字: primary key ...
- java在图片上写字
- Oracle 序列(sequence)的创建、修改及删除
1.Oracle 创建序列化:create sequence xxxx create sequence student_id minvalue --最小值 nomaxvalue --不设置最大值(由机 ...
- beego学习笔记(2)
BEEGO的几个特点: 简单化 RESTful 支持.MVC 模型,可以使用 bee 工具快速地开发应用,包括监控代码修改进行热编译.自动化测试代码以及自动化打包部署. 智能化 支持智能路由.智能监控 ...
- scala学习7--class、object、trait
scala语言中没有static成员存在,但是scala允许以某种方式去使用static成员这个就是伴生机制,所谓伴生,就是在语言层面上,把static成员和非static成员用不同的表达方式,cla ...