这个题需要两个dp,一个保存从i到j是否为回文串

另一个保存0到i的最小的分割

下面是我的效率不太高的代码

class Solution {
public:
int minCut(string s) {
vector<vector<bool> > dp(s.size(), vector<bool>(s.size(), false));
vector<int> res(s.size(), ); //辅助数组
for(int i = ; i < s.size(); i++)
{
for(int j = i, k = ; j < s.size(); j++, k++)
{
if(j-k <= )
dp[k][j] = s[k] == s[j] ? true : false;
else
dp[k][j] = (s[k] == s[j]) && (dp[k+][j-]);
}
} //如果0-i是回文串,那么不用切res[i]为0
//如果j-i是回文串,那么res[i]为res[j-1]+1
res[] = ;
for(int i = ; i < s.size(); i++)
{
res[i] = i;
for(int j = ; j <= i; j++)
{
if(dp[j][i])
res[i] = j == ? : min(res[i], res[j-] + );
}
}
return res[s.size()-]; }
};

132.leecode-Palindrome Partitioning II的更多相关文章

  1. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  2. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  3. 【LeetCode】132. Palindrome Partitioning II

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

  4. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

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

  5. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  6. 【leetcode】Palindrome Partitioning II

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

  7. [LeetCode] Palindrome Partitioning II 解题笔记

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

  8. 动态规划——Palindrome Partitioning II

    Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...

  9. LeetCode: Palindrome Partitioning II 解题报告

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

  10. leetcode132. Palindrome Partitioning II

    leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...

随机推荐

  1. ES6使用Set实现数组去重

    ES6里新添加了两个很好用的东西,Set和Array.from. Set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目. 常情况下,NaN === NaN 返回的是fa ...

  2. mysql数据库分区和分表

    转载自 https://www.cnblogs.com/miketwais/articles/mysql_partition.html https://blog.csdn.net/vbirdbest/ ...

  3. Python2和3版本对str和bytes类型的处理

    python2中字符串分为2种类型: 字节类型:str,字节类型,通过decode()转化为unicode类型 unicode类型:unicode ,通过encode转化为str字节类型 字节类型 和 ...

  4. ios 11 SDK 新特性 使用

    Xcode 9虽然已经出了一段时间,但考虑到一些第三方库的适配,就没有升级.现在有时间了就升级到 Xcode 9,随便学习一下新的小技巧.感觉很好用哦~ 一.Named Color 关于更换主题的一个 ...

  5. 两个Integer比较

    两个Integer类型比较不能使用==,要使用equals,   == 在-127~128是可以用的,超出这个范围就不行 public static void main(String[] args) ...

  6. Win10配置ADB工具教程

    1.在该网站下载adb工具 http://pcedu.pconline.com.cn/748/7481463.html 2. Win10怎么配置ADB环境?Win10怎么安装ADB工具?这想必是很多安 ...

  7. Android.mk学习

    2019-03-31 学习变量 $(call my-dir) /usr/bin2/android-ndk-r16/build/core $(CLEAR_VARS) /usr/bin2/android- ...

  8. 实用的JavaScript手册

    实用的JavaScript手册,收藏了,需要的时候可以翻阅 包含了 什么是JavaScript: JavaScript基础知识: JavaScript内置对象 JavaScript数据类型的操作方法 ...

  9. 转载 linux基本操作

    转载地址 https://segmentfault.com/a/1190000014840829 前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位.总的来说也算是 ...

  10. shell逻辑运算总结, 包括[[]]与[]的区别,&&与-a的区别,||与-o的区别

    1. 关于文件和目录 -f  判断某普通文件是否存在 -d  判断某目录是否存在 -b  判断某文件是否块设备 -c  判断某文件是否字符设备 -S  判断某文件是否socket(待修正) -L  判 ...