【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- ...
随机推荐
- Javascript数组操作及索引
1:清空数组最高效的做法 parentThis.PaperQuestionStrategiesList.length = 0; 2:push and pop parentThis.PaperQue ...
- 混沌数学之离散点集图形DEMO
最近看了很多与混沌相关的知识,并写了若干小软件.混沌现象是个有意思的东西,同时混沌也能够生成许多有意思的图形.混沌学的现代研究使人们渐渐明白,十分简单的数学方程完全可以模拟系统如瀑布一样剧烈的行为.输 ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- iOS开发-NSPredicate
Cocoa中谓词(Predicate)提供了一个通用的查询方式处理数据,可以获取和指定数据的过滤形式,Cocoa实际开发中可以是使用NSPredicate及其父类NSComparisonPredica ...
- mybatis结果的组装(springboot)
文主要解答一个问题,即如果bean没有setter,而且属性不是public的,mybatis的自动组装是否可以赋值成功的问题. 查询调用过程 DefaultSqlSession.selectList ...
- MySQL 的实时性能监控利器
操作系统及MySQL数据库的实时性能状态数据尤为重要,特别是在有性能抖动的时候,这些实时的性能数据可以快速帮助你定位系统或MySQL数据库的性能瓶颈,就像你在Linux系统上使用「top,sar,io ...
- matlab工作空间,变量的保存和载入
对于工作空间中变量的保存和载入可以使用save和load命令,详细的使用方法通过help指令获取(help save,help load). 两条指令最常用的情况为: 1.% 保存整个工作空间至指定 ...
- select设置高度的兼容问题
在IE678下,我们给select设置高度的话,里面的option无法居中,折中的兼容方式就是,我们给select的border:0:外面套一层div,这个div给他设置padding,让select ...
- poj1837--Balance(dp:天平问题)
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10773 Accepted: 6685 Descript ...
- 把普通java项目转换成maven项目
我使用的是eclipse,右键项目,Configure->Convert to Maven Project 然后就是jar包的引入了,如果jar包比较简单,建议从maven中拉取,这样便于后期升 ...