[Leetcode] palindrome partition 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",
Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.
题意:给定字符串s,分割s,使其每个子串都是回文串,至少需要切几下。
思路:动态规划。维护数组dp[ ] ,dp[ i ]代表(0 ,i)最小的回文切割。遍历字符串,当子串s.substr(0,i+1)(包括 i 位置的字符)是回文时,dp[i]=0,即表示不用切割,若不是回文,则令dp[ i ]=i ,表示至少要切 i 刀(有i+1个字符)。对于任意大于1的 i,如果s.substr(j,i+1)(j<=i,即遍历i之前的每个子串)
是回文时,转移方程:dp[i]=min(dp[i],dp[j-1]+1),因为若是,则只要增加一刀,就可以分为两个子串了;若不是,则取dp[i]=min(dp[i],dp[i-1]+1),因为,为回文串时,状态转移方程中j-1>=0,说明,首字符没有考虑,若出现如“efe”的情况时,dp[i] 的值就小于dp[ i-1 ]+1。代码如下:
class Solution {
public:
int minCut(string s)
{
int len=s.size();
if(len<) return ;
vector<int> dp(len,);
for(int i=;i<len;i++)
{
if( !isPalin(s,,i))
dp[i]=i;
}
for(int i=;i<len;++i)
{
for(int j=;j<i;j++)
{
if(isPalin(s,j,i))
dp[i]=min(dp[i],dp[j-]+);
else
dp[i]=min(dp[i],dp[i-]+);
}
}
return dp[len-];
}
bool isPalin(string &s,int l,int r)
{
int i=l,j=r;
while(i<j)
{
if(s[i] !=s[j])
return false;
i++;
j--;
}
return true;
}
};
博友Grandyang,使用DP简化了每次第(j,i+1)之间的是否为回文串的判断。
[Leetcode] palindrome partition ii 回文分区的更多相关文章
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- 【CF932G】Palindrome Partition(回文树,动态规划)
[CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
随机推荐
- dedesmc 手机端生成静态页
dedesmc 手机端生成静态页 1.首先下载插件,下载地址:https://pan.baidu.com/s/1Nfx_KBYuxRkZ7VzoPxy28g 密码:83x7 2.进入 dedecms ...
- mysql数据库的基本使用命令总结
mysql数据库是一个常用的关系型数据库 关系型数据库核心元素有哪些? 主键:特殊字段,用来唯一标识记录的唯一性 字段:数据列 记录:数据行 数据表:数据行的集合 数据库:数据表的集合 安装.启动.停 ...
- angularjs路由不断刷新当前页面
最近做项目遇到个问题,使用angular-route的时候,第一次点击 [按钮 a]会进入按钮a对应的控制器,接着再次点击a按钮的的时候就不会进入控制器了.我想要的效果是每次点击都能进入control ...
- python装饰器+递归+冒泡排序
冒泡排序 li = [33, 2, 10, 1,23,23523,5123,4123,1,2,0] for k in range(1,len(li)): for i in range(len(li) ...
- Leecode刷题之旅-C语言/python-26.删除数组中的重复项
/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...
- LeetCode 二叉树的层次遍历 C++
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层 ...
- 005---json & pickle
json & pickle 什么是序列化 序列化是指把内存里的数据类型转变成字符串,以便使其能存储在硬盘和网络传输.因为只能接收bytes类型. 为什么要序列化 持久化存储 分类 - json ...
- 016---Django的ModelForm
对于forms组件虽然可以帮我们渲染html页面,也可以做校验,但是,保存到数据库要取各字段的值,还要手动保存.所以引入了一个新的组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把 ...
- [Cracking the Coding Interview] 4.4 Check Balanced
Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...
- windows下subversion服务器搭建
一.下载subversion服务器端和客户端软件 1.subversion下载地址:http://subversion.tigris.org/ 2.svn比较流行的客户端Tortoisesvn下载地址 ...