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) ☆的更多相关文章

  1. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  2. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  3. 【leetcode】Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  4. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  6. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 【Leetcode】【Medium】Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. $(document).ready(){}、$(fucntion(){})、(function(){})(jQuery)onload()的区别

     1.首先说JQuery的几个写法  $(function(){     //do someting   });   $(document).ready(function(){     //do so ...

  2. Google翻译请求(难点是tk参数)

    业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...

  3. android 读取SQLite android could not open the database in read/write mode错误

    由于AndroidManifest.xml文件中uses-permission没有设置权限问题 <uses-permission android:name="android.permi ...

  4. PHP基础之 string 字符串函数

    /*=================常用字符串处理函数================== ltrim();        //去掉字符串左边的空格 rtrim();         //去掉字符串 ...

  5. 【python网络编程】使用rsa加密算法模块模拟登录新浪微博

    一.基础知识 http://blog.csdn.net/pi9nc/article/details/9734437 二.模拟登录 因为上学期参加了一个大数据比赛,需要抓取数据,所以就想着写个爬虫抓取新 ...

  6. NGUI Sprite 和 Label 改变Layer 或父物体后 未更新深度问题

    using UnityEngine; using System.Collections.Generic; /// <summary> /// Sprite is a textured el ...

  7. [POJ2892]Tunnel Warfare

    [POJ2892]Tunnel Warfare 试题描述 During the War of Resistance Against Japan, tunnel warfare was carried ...

  8. [codevs1001]舒适的路线

    [codevs1001]舒适的路线 试题描述 Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,-,N),这些景点被M(0 ...

  9. 国外开源的PACS服务器

    国外开源的PACS服务器 罗朝辉(http://www.cnblogs.com/kesalin/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 名称:Dcm4che评级:★ ...

  10. Panabit安装配置笔记

    最近研究了linux下基于FREEBSD的开源流控软件Panabit,感觉功能还不错,单位公司如果经费不足的朋友需要做内网流控可以使用这款软件,最新免费版ISO镜像仅支持网桥模式和旁路模式,256个并 ...