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 ...
随机推荐
- [LeetCode]14.最长公共前缀(Java)
原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...
- Intellij IDEA出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver处理办法
菜单-->File-->project structure-->Modules-->Dependencies-->添加MySQL的驱动包:mysql-connector- ...
- suse 12 安装git客户端
suse-linux:~ # zypper addrepo http://download.opensuse.org/repositories/devel:/tools:/scm/SLE_12_SP5 ...
- 【数据共享】基于Landsat提取的全球河网(河宽)GDWL数据库
GRWL数据库,全称Global River Widths from Landsat Database,是由Allen, George H & Pavelsky. Tamlin M等人基于La ...
- [Golang]一些书城项目中出现错误的原因和解决办法(一)
跟着B站尚硅谷的GoWeb教程写书城项目,整理一下自己写的时候出现的错误和解决办法. 错误一:cartItem中只能加入一种书,SQL语句没有问题,但是购物车中的总金额和总数量正确: 原因:cartI ...
- can_has_stdio?
得到一个用±<>这样符号组成的五角星,结合题目stdio,估计是c语言编译后的文件 查到BrianFuck语言,找个在线编译器或者找到编译码(c++)得到flag 在线编译网站 brain ...
- Spring Boot+RabbitMQ 通过fanout模式实现消息接收(支持消费者多实例部署)
本文章适用的场景:同一条消息可以被多个消费者同时消费.注意:当消费者多实例部署时,会轮询消费消息.网上有大量的的案例展示:P生产一条消息,消费者服务C中建立Q1和Q2两个队列共同消费.但极少的材料展示 ...
- 商业智能bi行业现状,BI应用的3个层次
商业智能bi行业现状.传统的报表系统技术上已经相当成熟,大家熟悉的Excel等都已经被广泛使用.但是,随着数据的增多,需求的提高,传统报表系统面临的挑战也越来越多. 1. 数据太多,信息太少 密密麻 ...
- 案例三:shell统计ip访问情况并分析访问日志
题目要求 有日志 1.log,部分内容如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com"/secco ...
- Python:Scrap爬虫过程中遇到的各种错误
1.KeyError: 'Spider not found: BDS' 原因:settings.py中缺少了几项与spider名字配置相关的项: BOT_NAME = 'BDS' SPIDER_MOD ...