刷题5. Longest Palindromic Substring
一、题目说明
Longest Palindromic Substring,求字符串中的最长的回文。
Difficuty是Medium
二、我的实现
经过前面4个题目,我对边界考虑越来越“完善”了。
总共提交了5次:
第1、2次:Wrong Answer
主要是"cbbd"错误了,重复的判断逻辑上出了点小问题
第3、4次: Time Limit Exceeded
我本地代码运行没问题的,但是提交后,报错。给了一个超长的用例:
321012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210123210012321001232100123210123
我本地运行是no problem,但是提交后不行。
我继续优化代码,第5次提交终于对了。本次提交代码性能如下:
Runtime: 488 ms, faster than 5.81% of C++ online submissions for Longest Palindromic Substring.
Memory Usage: 8.7 MB, less than 91.03% of C++ online submissions for Longest Palindromic Substring.
完整代码如下
#include<iostream>
#include<string>
using namespace std;
class Solution{
public:
string longestPalindrome(string s){
int maxLength=1;
string maxStr = s.substr(0,1);
int len = s.length();
if(len<=1){
return s;
}else{
int i = 0;
while(i<len){
int j = len-1;
while(j>i){
int t=i,k=j;
while(t<k && s[t]==s[k]){
t++;
k--;
}
if(t==k || t==k+1 && s[t]==s[k]){
if(j-i+1>maxLength){
maxLength = j-i+1;
maxStr = s.substr(i,j-i+1);
}
break;
}
j--;
}
i++;
}
}
return maxStr;
}
};
int main(){
Solution s;
cout<<s.longestPalindrome("ac")<<endl;
cout<<s.longestPalindrome("babad")<<endl;
cout<<s.longestPalindrome("cbbd")<<endl;
cout<<s.longestPalindrome("321012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210123210012321001232100123210123");
return 0;
}
刷题5. Longest Palindromic Substring的更多相关文章
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- 5. Longest Palindromic Substring 返回最长的回文子串
[抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
随机推荐
- HttpRequestException encountered解决方法
每次pull代码的时候,总是要输入账号,密码,百度了一下HttpRequestException encountered错误 发现是Github 禁用了TLS v1.0 and v1.1,必须更新Wi ...
- c# Gridview 自动分页功能 解决后面页面不显示问题
操作步骤: 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 任意数值(默认为10) 3.更改GrdView控件的Pa ...
- SpringBoot整合WEB开发--(七)注册拦截器
1.创建一个拦截器类实现HandlerInterceptor接口,重写其中的3个方法,这拦截器中方法的执行顺序为:preHandle--Controller--postHandle--afterCom ...
- vue -bug1
打包问题: 1.在终端用 npm run build 如果想要在本地资源也能访问 2.npm install -g http-server 3.配置好如下 3.1 config->index. ...
- Openstack 简单梳理,(自用 慎点)
这个图里面的彩色方块,就是OpenStack最核心的组件. 推荐几个大咖,大家可以百度找他们的博客来看:陈沙克.何明桂.孔令贤,Cloudman.
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)
题目链接:https://codeforces.com/contest/1265/problem/E 题意:有n面镜子,你现从第一面镜子开始询问,每次问镜子"今天我是否美丽",每天 ...
- laravel框架实现发送邮件的功能
1.在config 下的mail.php中配置(配置后面的两个就行了) 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', '7623018 ...
- JS高级---函数声明和函数表达式的区别
函数声明和函数表达式的区别 多用函数表达式 var ff=function(){}; //函数声明 // // if(true){ // function f1() { // console.log( ...
- html集合
<!DOCTYPE> //声明文档类型 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> ...