这个题需要两个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. 使用SQL语句查询表及表字段类型说明

    今天突然遇到有人要数据库表及表字段说明,数据库表太多又不能一个个表去找,就想想SQL是否能直接查询出来. 经过查询资料,加上一些自己的一些调整写了一个sql语句,在此记录一下,以方便日后查找使用. S ...

  2. 手工脱壳之FSG压缩壳-IAT表修复

    目录 一.工具及壳介绍 二.脱壳 2.1.单步跟踪脱壳 2.2.IAT修复 三.程序脱壳后运行截图 四.个人总结 五.附件 一.工具及壳介绍 使用工具:Ollydbg.PEID.ImportREC.L ...

  3. linux-kernel-4.4 移植 (1)启动

    开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...

  4. Linux静态设置CentOS 7虚拟机的IP

    进入root ,输入命令:# vi /etc/sysconfig/network-scripts/ifcfg-ens33 .将DHCP协议获取IP,改为static静态,加上想要设置的IPADDR即可 ...

  5. IDEA 上 Tigase8.0 源代码编译运行

    Tigase交流群 :310790965 一些小伙伴们经常问Tigase8 check下来的源代码怎么跑起来,因为我不能及时一 一回答,所以还是写个博文来演示一下吧,一般针对新手而言,老手的话,就跳过 ...

  6. Redhat 7 开通防火墙端口

    1.查看防火墙状态,root用户登录,执行命令systemctl status firewalld 2.开启防火墙:systemctl start firewalld 3.关闭防火墙:systemct ...

  7. Centos 安装 android sdk(转)

    原文地址: https://blog.csdn.net/kai_1215/article/details/80731099 这个后面有个指令没有运行起来,我做了一些修改: 原文:sdkmanager ...

  8. windows socket 文件下载上传

    socket 图片 文件 下载上传  数据库 线程池  存入图片 等

  9. JS require and import

    作者:寸志链接:https://www.zhihu.com/question/56820346/answer/150724784来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. Unique Letter String LT828

    A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...