这个题需要两个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. js及vue监听键盘回车事件

    js document.onkeydown = (event) => { var e = event || window.event; if(e && e.keyCode==13 ...

  2. 【Spring学习】Spring的源码解析之路 ——【step1】基础环境配置 + IDEA快捷键整理

    前导: 本次主要是关于“基础环境搭建”的内容,分为三个部分,(1)查看源码和项目需要的开发集成环境Intellig IDEA的使用,(2)如何导入源码,(3)在这个过程中梳理出的快捷键 正文:==== ...

  3. TZOJ 2703 Cow Digit Game(sg博弈)

    描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve victor ...

  4. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-7主节点CM安装子节点Agent配置

    主节点安装cloudera manager 准备工作:下载CM和mysql连接驱动包: CM各版本下载地址:http://archive.cloudera.com/cm5/cm/5/ 从里面选择:ht ...

  5. Linux-Centon7常用命令

    查看本机IP # ip addr 进入目录 # cd /xxx/xxx 编辑文件,打开文件后,按“Insert”键,进入输入模式(最下面会显示INSERT),将ONBOOT选项改为yes,然后按“Es ...

  6. 记录一次网站邮箱(STMP)被恶意伪造事件

    网站运行了一段时间了,最近用户增加的比较多,突然有用户反馈说遇到骗子了,给我看了他跟骗子的邮件记录,发现骗子竟然可以用我们域名的邮件发邮件,真是被惊吓到了,赶紧开始查找问题. 首先怀疑是企业邮箱密码泄 ...

  7. stark组件开发之组合搜索页面效果和 URL

    页面效果,只是样式.这个好解决!yield 的时候. 返回几个样式出去就好了! 并且前端写上一些样式的css {% if search_group_row_list %} <div class= ...

  8. c#Dapper mysql按时间段查询和过滤

    #endregion /// <summary> /// 根据条件获取集合 /// </summary> /// <param name="id"&g ...

  9. 7K - find your present (2)

    In the new year party, everybody will get a "special present".Now it's your turn to get yo ...

  10. Program Hyundai i20 Smart Key with VVDI & Xtool X100 Pad2

    How-to: generate and program Hyundai i20 smart key with VVDI Mini Key Tool and XTOOL X100 PAD2. Step ...