[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 首先确定该数字的 ...
随机推荐
- dedecms添加/编辑文章如何把附加选项去掉默认勾选状态
1.去掉添加时默认勾选状态. 在 系统->系统基本参数->其它选项 中,如图中的三个选项选择否即可. 设置完后可以看到添加时已经默认不勾选,但是编辑文章时还是默认勾选状态. 2.去掉编辑时 ...
- Hadoop(6)-HDFS的shell操作
1.基本语法 使用 hadoop fs 具体命令 或者 hdfs dfs 具体命令 hadoop命令的shell源码 hdfs命令的shell源码 由此可见,这两个命令最后都是执行的一个jav ...
- ctf题目writeup(1)
2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...
- 002---time & datetime
time & datetime 时间模块 分类 时间戳 时间字符串 时间元祖 定义 UTC:格林威治时间,世界标准时间,中国(UTC + 8) 时间戳:1970-01-01 0:0:0 开始按 ...
- debounce、throttle、requestAnimationFrame
今天review同事代码,代码实现了返回顶部的功能,用到了lodash库中的throttle,我看着眼生,于是乎去看了下lodash文档,然后牵出了debounce,具体的知识点,这里不再赘述,底部的 ...
- 隐式Dijkstra:在状态集合中用优先队列求前k小
这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...
- vue 项目如何使用微信分享接口
首先做微信网页都要接入微信sdk: 安装sdk npm install weixin-js-sdk --save 具体可以查看微信公众平台技术文档:https://mp.weixin.qq.com/w ...
- php杂记——1(基础知识与文件读写)
1.变量前面需要加美元符号"$",常量则不需要: define('PRICE',100); echo PRICE; 2.用一个变量的值作为另一个变量的名称可以得到类似C中的指针变量 ...
- 【java并发编程实战】第五章:基础构建模块
1.同步容器类 它们是线程安全的 1.1 vector和hashtable. 和Collections.synchronizeXxx()一样.实现方式就是在每个方法里面加入synchronize代码块 ...
- 转型、java基础之Java变量命名规范 (转载)
向上转型:基类引用指向导出类(派生类)的对象(实例)向下转型:导出类的引用指向基类的对象(实例) 重点:向下转型只有将该引用的导出类的向上转型后向下转型,运行时才不会报错 Java是一种区分字母的大 ...