leetcode刷题1--动态规划法回文串2
题目是:
Given a string s,partition s such that every substring of the partition is a palindrome
Return tthe mininum cuts needed for a palindrome partitioning of s.
For example,given s="aab"
Return 1 since the palindrome partition["aa","b"]could be produced using 1cut
我的思路:
用动态数组dp[i]来记录当i+1个字符时,需要的分隔次数。
当前i个字符是回文串时,则dp[i]=0
当前i个字符不是回文串时,则dp[i]先置为i(i+1个字符需要的最大分割次数)
这个时候的dp[i]的值就由前i个字符来决定,用j来分隔前i个字符
则dp[i]=min{dp[i],dp[j-1]+1(当前j个字符是回文串时)||dp[j-1]+1+i-j(当前j个字符不是回文串时)}
代码如下:
int minCut(string s){
vector<int>dp(s,size(),s.size()-1)//默认值为字符串的最大分割次数
for(int i=0;i<s.size();i++){
dp[i]=Is_palindrome(s.substr(0,i+1))?0:i;//判断i+1个字符是不是回文串
if(dp[i]==0)continue;//如果是则继续循环
for(int j=1;j<=i;j++){
if(Is_palindrome(s,substr(j,i+1-j))){
dp[i]=min(dp[i],dp[j-1]+1);
}
else{
dp[i]=min(dp[i],dp[j-1]+1+i-j);
}
}
}
return dp[s.size()-1];
}
//判断是否是回文串
bool Is_palindrome(string s){
int begin=0;
int end=s.size()-1;
while(begin<end){
if(s[begin]<s[end]){
begin++;
end--;
}
else break;
if(begin>=end)return true;
return false;
}
}
做这条题目遇到的坑:
在判断回文串的时候,我首先想到的是STL中算法中的reverse函数,但是做的时候还是拿不准reverse函数的参数,于是采取了上面代码的方式,要是严格说起来的话也不是很难,清晰易懂,看来写代码不能拘泥于现成的东西。
leetcode刷题1--动态规划法回文串2的更多相关文章
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- 【leetcode 简单】 第九十六题 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 从0打卡leetcode之day 6--最长回文串
题目描述 给定一个字符串 s,找到 s中最长的回文子串.你可以假设 s 的最大长度为1000. 示例1 输入: "babad" 输出: "bab" 注意: &q ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 牛客寒假算法基础集训营4 I题 Applese 的回文串
链接:https://ac.nowcoder.com/acm/contest/330/I 来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode.字符串.409最长回文串-Java
1. 具体题目 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设 ...
- [LeetCode] Largest Palindrome Product 最大回文串乘积
Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...
随机推荐
- 手写一个springboot starter
springboot的starter的作用就是自动装配.将配置类自动装配好放入ioc容器里.作为一个组件,提供给springboot的程序使用. 今天手写一个starter.功能很简单,调用start ...
- Dubbo扩展点应用之一filter及@Activate自激活使用
与很多框架一样,Dubbo也存在拦截(过滤)机制,可以通过该机制在执行目标程序前后执行我们指定的代码.Dubbo中Filter只是Dubbo提供的可自定义扩展的扩展点之一.通过该扩展点地理解,可以触类 ...
- Linux性能优化之内存性能统计信息
关于内存的概念及其原理在任何一本介绍操作系统的书本中都可以查阅到. 理论放一遍,在Linux操作系统中如何查看系统内存使用情况呢?看看内存统计信息有哪些维度. 一.内存使用量 详细使用方法,man f ...
- 中国首个进入谷歌编程之夏(GSoC)的开源项目: Casbin, 2022 年预选生招募!
Casbin 明日之星预选生计划-Talent for Casbin 2022 "Casbin 明日之星预选生计划-Talent for Casbin 2022"是什么? &quo ...
- 在 CentOS 或 RHEL 系统上检查可用的安全更新的方法
当你更新系统时,根据你所在公司的安全策略,有时候可能只需要打上与安全相关的补丁.大多数情况下,这应该是出于程序兼容性方面的考量.那该怎样实践呢?有没有办法让 yum 只安装安全补丁呢? 答案是肯定的, ...
- 【Windows 访问控制】八、安全主体和安全对象
安全主体(security principal)? 安全主体是任何可通过操作系统进行身份验证的实体,例如用户帐户.计算机帐户.在用户或计算机帐户的安全上下文中运行的线程或进程,或者这些帐户的安全组. ...
- 每日一算之变位词(C#)
今天看编程珠玑里面,看到一个关于查找变位词的题目,大概意思如下:post,stop,tops这几个是变位词,找出类似的这些词语来. 解题思路一:既然是变位词,1.他们的长度一定是一致的:2.还有就是他 ...
- tomcat启动错误:Error running 'Tomcat 9.0.34': Address localhost:1099 is already in use
解决方法博客地址:https://blog.csdn.net/weixin_46697202/article/details/105782670
- 【C#版本】微信公众号模板消息对接(二)(图文详解)
本篇文章承接上一篇文章内容,点击此段文字传送至上一篇文章. 特此说明:本篇文章为个人原创文章,创作不易,未经作者本人同意.许可等条件,不得以任何形式搬运.转载.抄袭(等包括但不限于上述手段)本文章,否 ...
- css3渐变色字体
\3cspan id="mce_marker" data-mce-type="bookmark">\3c/span>\3cspan id=" ...