【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II
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.
从后往前构造二维数组isPalin,用于存储已经确定的回文子串。isPalin[i][j]==true代表s[i,...,j]是回文串。
在构造isPalin的同时使用动态规划计算从后往前的最小切分数,记录在min数组中。min[i]代表s[i,...,n-1]的最小切分数。
(上述两步分开做会使得代价翻倍,容易TLE)
关键步骤:
1、min[i]初始化为min[i+1]+1,即初始化s[i]与s[i+1]之间需要切一刀。这里考虑边界问题,因此min数组设为n+1长度。
2、从i到n-1中间如果存在位置j,同时满足:(1)s[i,...,j]为回文串;(2)1+min[j+1] < min[i]。
那么min[i]=1+min[j+1],也就是说一刀切在j的后面比切在i的后面要好。
class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > isPalin(n, vector<bool>(n, false));
vector<int> min(n+, -); //min cut from end
for(int i = ; i < n; i ++)
{
isPalin[i][i] = true;
}
for(int i = n-; i >= ; i --)
{
min[i] = min[i+] + ;
for(int j = i+; j < n; j ++)
{
if(s[i] == s[j])
{
if(j == i+ || isPalin[i+][j-] == true)
{
isPalin[i][j] = true;
if(j == n-)
min[i] = ;
else if(min[i] > min[j+]+)
min[i] = min[j+] + ;
}
}
}
}
return min[];
}
};

【LeetCode】132. Palindrome Partitioning II的更多相关文章
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- OTL翻译(2) -- OTL流的概念
OTL流的概念 任何的SQL语句.SQL语句块或存储过程,都是通过输入与输出变量进行处理参数与结果的. 如: 例1:一个SELECT语句把标量的输入变量作为WHERE子句部分的条件:同时SELECT部 ...
- Spring 事务模板
最近项目开发中需要用到单机事务,因为项目中使用了Spring和Mybatis框架,所以通过Spring来进行事务的管理,并且记录一下事务配置的过程 第一步:配置DataSource <!-- 发 ...
- require.js 最佳实践
require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(require.js), Javascript模块化工具require.js教程,RequireJ ...
- 【Gson】简介 文档 基本使用 示例
简介 new TypeToken<List<Person>>() {}.getType() 1 1 1 new TypeToken<List<Person> ...
- 修改一行SQL代码 性能提升了100倍
在PostgreSQL中修改了一行不明显的代码,把(ANY(ARRAY[...]) 改成 ANY(VALUES(...))),结果查询时间从20s变为0.2s.最初我们学习使用 EXPLAN ANAL ...
- 笔记本建立wifi热点的实用详细步骤
准备工作: (1) 首先要打开开始菜单--->搜索“services.msc” 并打开(或者用win+R快捷键打开“运行”输入“service.msc”,点击确定)--->找到“WLAN ...
- 主成分分析(PCA)
相对与网上很多人分享的有关PCA的经历,我第一次接触PCA却不是从人脸表情识别开始的,但我所在的实验室方向之一是人脸的研究,最后也会回到这个方向上来吧. PCA(principal component ...
- Asp.Net 之 前台绑定常用总结
1.<%= 变量名 %> 里面放的是后台定义的变量名,如: <div> <p> <%= DateTime.Now.ToString() %></p ...
- SVN diff 笔记
SVN diff命令在实际中经常使用,在此记录使用点滴. #对比工作文件与缓存在.svn的“原始”拷贝: svn diff #显示工作文件和服务器版本2的不同: svn diff -r 2 #显示分支 ...
- IDE、SATA、SCSI、SAS、FC、SSD硬盘类型介绍
参考于:http://blog.csdn.net/tianlesoftware/article/details/6009110 目前所能见到的硬盘接口类型主要有IDE.SATA.SCSI.SAS.FC ...