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. flask 知识点总结

    ============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...

  2. Ansible简明使用手册

            Ansible使用简明手册 1.简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric ...

  3. hash-6.CopyOnWriteArrayList

    1.ArrayList的add方法 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount ...

  4. 剑指Offer 变态跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   其实就是斐波那契数列问题. 假设f(n)是n个台阶跳的次数. f(1) = ...

  5. JAVA_HOME path classpath 以及cmd编译运行java代码

    JAVA_HOME PATH CLASSPATH 三者的区别:安装完jdk之后,首先在环境变量里面添加JAVA_HOME ,例如安装路径为C:\Program Files\Java\jdk1.6.0_ ...

  6. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  7. python——tuple元组

    >>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', ...

  8. c语言数据问题

    变量都有作用域,链接属性,和存储类型3个属性,这三个属性决定了变量的作用域和生存期的问题 在c语言中包含4中类型, 整形 浮点型 指针 聚合类型(数组,结构体等) ------------------ ...

  9. Linux 文件rwx权限问题 chmod 777 XXX 任何人拥有最高权限

    在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限.ls -l:得到-rw-r--r-- 1 apple users 2254 2006-05-20 13 ...

  10. python的变量作用域问题

    偶然掉进了一个坑里.仔细分析了下原因.原来是变量作用域的问题.简单抽象如下: id=1 #许多行代码 [id for id in range(10)] #许多行代码 if id!=1: #做一些事情 ...