一、题目说明

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的更多相关文章

  1. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  2. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  3. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  4. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  5. leetcode-【中等题】5. Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  6. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  7. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...

  8. 【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 ...

  9. 5. Longest Palindromic Substring 返回最长的回文子串

    [抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

随机推荐

  1. OpenCV3+VS2015 经常出现debug error abort()has been called问题

    方案1:图片路径错误:查看imread的路径

  2. IDEA 在SVN上更新代码错误: Error:Server SSL certificate rejected

    在IDEA中更新代码到SVN中 ,出现了   Error:Server SSL certificate rejected    ---服务器的SSL证书  的错误 之前在网上有找过一些相关的做法,但是 ...

  3. 高效完成R代码

    为什么R有时候运行慢? 参考https://www.cnblogs.com/qiaoyihang/p/7779144.html 一.为什么R程序有时候会很慢? 1.计算性能的三个限制条件 cpu ra ...

  4. 巨杉Talk | 拒绝数据碎片化,原生分布式数据库灵活应对数据管理需求

    2019年7月19-20日,以“运筹帷幄,数揽未来”为主题的DAMS中国数据智能管理峰会在上海青浦区成功举办.在DAMS峰会上,巨杉数据库为大家带来了题为“云架构下的分布式数据库设计与实践”的主题分享 ...

  5. VScode 编辑器快捷键被占用

    在家办公这个开发环境实在是让人受不鸟 .mysql .vscode 对连QQ都没有运行还是不行.排查最终发现了这个家伙—搜狗输入法.微软输入法 这两个把系统快捷键占得那个全 我这个全局搜索死活用不了, ...

  6. [一本通学习笔记] KMP算法

    KMP算法 对于串s[1..n],我们定义fail[i]表示以串s[1..i]的最长公共真前后缀. 我们首先考虑对于模式串p,如何计算出它的fail数组.定义fail[0]=-1. 根据“真前后缀”的 ...

  7. word doc转pdf

    from win32com.client import constants, gencache # TODO pip install pywin32 -i http://mirrors.aliyun. ...

  8. python面试的100题(8)

    企业面试题 15.python新式类和经典类的区别? (在Python 2及以前的版本中,由任意内置类型派生出的类(只要一个内置类型位于类树的某个位置),都属于“新式类”,都会获得所有“新式类”的特性 ...

  9. break continue goto

    break:跳出本层循环:continue:结束本次循环,并不跳出循环:goto:转移到指定位置,无条件转移:

  10. Python入门3 —— 基本数据类型

    一:为何变量值(记录的数据)要有类型呢? 1.既然可以记录事物的状态,为什么要分类型呢? 变量值是来记录事物状态的, 而事物的状态是多种多样的, 所以对应着就要应该用不同类型的值去记录这些状态. 二: ...