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 ...
随机推荐
- suse 12 二进制部署 Kubernetets 1.19.7 - 第11章 - 部署coredns组件
文章目录 1.11.0.部署coredns 1.11.1.测试coredns功能 suse 12 二进制部署 Kubernetes 集群系列合集: suse 12 二进制部署 Kubernetets ...
- 【故障公告】k8s 开船记:增加控制舱(control-plane)造成的翻船
春节期间我们更换了 kubernetes 生产集群,旧集群的 kubernetes 版本是 1.17.0,新集群版本是 1.23.3,新集群上部署了 dapr,最近准备将更多独立部署的服务器部署到 k ...
- Spring容器变化之SmartLifecycle,LifecycleProcesso接口详述
Spring Boot run方法启动后相应的服务也随之启动,这个操作很妙.使用者都不用关心什么服务怎么启动,不管多少个服务怎么启动只要符合Spring Boot的启动规则都可以使用其run方法同一启 ...
- 五、MyBatis缓存初体验
缓存就是内存中的数据,常常来自对数据库查询结果的保存,使用缓存, 我们可以避免频繁的与数据库进行交互, 进而提高响应速度. 一级缓存初体验(session,默认打开) 同一查询执行两次以上:selec ...
- 在k8s中使用性能分析神器:arthas
Arthas(阿尔萨斯)是阿里巴巴开源的性能分析神器. k8s中使用arthas的三种方式 [bak]https://www.cnblogs.com/uncleyong/p/15498842.html ...
- kali linux更新msf 报错Unable to find a spec satisfying metasploit-framework (>= 0) in the set. Perhaps the解决办法
首先换更新源 :vim /etc/apt/sources.list deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free co ...
- 命令行窗口cmd:访问C盘根目录和其他盘
1:访问C盘: cd.. 往前推一个目录 以此类推,多用几次cd..即可退回到根目录 2:访问桌面文件夹 由于cmd命令行中>号的存在我们不能直接访问其他文件,所以用cd将>删去 所以 用 ...
- Pandas:各种错误
1.输出为CSV文件时,Permission denied 原因可能是: (1).构建DataFrame时没有写index参数 (2).用Dict构建最开始的数据时,value没有写成List的形式, ...
- Python:Dict
0.运算符 in:检查字典中是否有某个key 'a' in {'a':1,'b':2} True 提取其中Key对应的Value: d={'1':'A','2':'B','3':'C'} d['2'] ...
- 关于电脑上已安装SqlServer2005再安装SqlServer23008r2的处理情况
安装SqlServer2008r2可参考这个回答,带图很详细 https://xinzhi.wenda.so.com/a/1518683577611182 1.先修改2005注册表.win+R打开运 ...