【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
最少切几刀,才能让一个字符串的每个部分都是回文。
思路:
用cut[i] 存储从s[0] ~ s[i - 1] 的子字符串,最短切几刀。
为了方便令 cut[0] = -1
只有一个字符时不需要切刀 cut[1] = 0
其他情况下,依次假设从(-1 ~ i - 2)处切刀,如果 s切刀的后半部分是一个回文, cut[i] = cut[j] + 1; cut[i]取所有切法中数值最小的那个
代码如下:这是O(n3)方法,结果超时了....
class Solution {
public:
int minCut(string s) {
vector<int> cut(s.length() + , );
cut[] = -;
for(int i = ; i <= s.length(); i++)
{
int minNum = s.length();
for(int j = ; j < i; j++)
{
if(isPalindrome(s.substr(j, i - j)))
{
int num = cut[j] + ;
minNum = min(num, minNum);
}
}
cut[i] = minNum;
}
return cut[s.length()];
} bool isPalindrome(string s)
{
int i = , j = s.length() - ;
while(i < j)
{
if(s[i] != s[j]) return false;
i++; j--;
}
return true;
}
};
各种截枝都通过不了,只好看别人的思路,原来可以在判断回文这里下工夫,我是每次都自己判断一遍是不是回文,实际上可以将之前求过的回文信息保存下来,方便后面的判断。
O(N2)解法
class Solution {
public:
int minCut(string s) {
vector<int> cut(s.length() + , );
vector<vector<bool>> isPalindrome(s.length() + , vector<bool>(s.length() + , false));
cut[] = -;
for(int i = ; i <= s.length(); i++)
{
int minNum = s.length();
for(int j = i - ; j >= ; j--)
{
if((s[j] == s[i-]) && (i - - j < || isPalindrome[j + ][i - ]))
{
isPalindrome[j][i - ] = true;
minNum = min(cut[j] + , minNum);
}
}
cut[i] = minNum;
}
return cut[s.length()];
}
};
还有更厉害的,上面的方法保存了判断回文的信息,这有一个不需要保存的,速度非常快
class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<int> cut(n+, ); // number of cuts for the first k characters
for (int i = ; i <= n; i++) cut[i] = i-;
for (int i = ; i < n; i++) {
for (int j = ; i-j >= && i+j < n && s[i-j]==s[i+j] ; j++) // odd length palindrome
cut[i+j+] = min(cut[i+j+],+cut[i-j]); for (int j = ; i-j+ >= && i+j < n && s[i-j+] == s[i+j]; j++) // even length palindrome
cut[i+j+] = min(cut[i+j+],+cut[i-j+]);
}
return cut[n];
}
};
【leetcode】Palindrome Partitioning II(hard) ☆的更多相关文章
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 【leetcode】Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【Leetcode】【Medium】Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [Leetcode][JAVA] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- CSS浮动属性Float介绍
#cnblogs_post_body h6 {font-size: 16px;font-weight: bold;} 什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中 ...
- iOS设置分割线从边框顶端开始
好方法,本来是在xib里面设置自定义分割线位置,结果还是差15像素,该方法亲测好使. IOS8 设置TableView Separatorinset 分割线从边框顶端开始 (转) 在ios8上 [Ta ...
- 浅谈checkpoint与内存缓存
事务日志存在检查点checkpoint,把内存中脏数据库写入磁盘,以减少故障恢复的时间,在此之前有必要提下SQL Server内存到底存放了哪些数据? SQL Server内存使用 对SQL Serv ...
- 全文检索引擎Solr系列——整合MySQL、MongoDB
MySQL 拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面 ...
- codeblocks+Mingw 下配置开源c++单元测试工具 google test
google test 是google的c++开源单元测试工具,chrome的开发团队就是使用它. Code::Blocks 12.11(MinGW 4.7.1) (Windows版)Google T ...
- JS数组的基本用法
JS数组的用法包括创建.取值赋值.添加以及根据下标(包括数值或字符)来移除元素等等,在本文中将为大家详细介绍,感兴趣的朋友可以参考下. 1.创建数组: //1.1直接创建一个数组对象 var arra ...
- 如何在IE8设置透明背景
background:rgba(0,0,0,0.5);filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#7F00000 ...
- Pcserver+oracle10g+rac
成本的相对廉价,技术的成熟,功能的强大此方案将越来越受中小企业的青睐. 一.实验前准备 虚拟机版本:Vwareserver1.0.6 Linux版本:redhat5.5enterprise服务 ...
- xcode免证书开发
工程和target的code sign 选择 ios developer 工程的team选择为none就ok 推送测试 或者 正式发布的时候要改回来, 有一个严重的缺陷, 就是打包出来的ipa程序有时 ...
- 关于man和help的区别
help 是内部命令的帮助,比如cdman 是外部命令的帮助,比如ls