刷题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 ...
随机推荐
- 零基础自学Python是看书还是看视频?
很多人都碍于Python培训班的高昂费用和有限的空余时间都选择自学Python,但是没有老师帮助,显得有些迷茫,不知应该从何处学起,也不知识看书学习还是应该看视频学习.本就来谈谈这个话题. 我们先 ...
- Docker+JMeter+InfluxDB+Grafana从容器内部发起压测
1.自由定制JMeter镜像: Dockerfile文件: FROM java:8# 基础镜像 MAINTAINER yangjianliang <526861348@qq.com># 作 ...
- STM 32 内部功能回顾
EXTI 外部中断 NVIC 嵌套的向量式中断控制器 AHB 是高级高性能内部总线,主要是用在CPU.DMA.DSP(数字信号处理) APB 是外围总线,I2C. 串口 APB 分为高速APB2( ...
- 第一篇 网站基础知识 第3章 DNS的设置
第3章 DNS的设置 3.1 DNS解析 3.2 Windows 7设置DNS服务器 3.3Windows设置本机域名和IP的对应关系 在自己的电脑里也可以设置域名和IP的对应关系,具体设置是在C:\ ...
- C语言注释符号
同学们认为注释很简单,那我来看看下面的代码是否正确? 1.似是而非的问题 int main() { int/*...*/i; char* s = "abcdefgh //hijklmn&qu ...
- 修改linux的MAC地址
命令修改 步骤如下,但不唯一: 1.临时性的修改: ①依次输入以下命令: /sbin/ifconfig eth0 down /sbin/ifconfig eth0 hw ether 00:0C:29: ...
- 一键安装各个版本boost库(无需编译)
1.NuGet 最简单的,用VS自带的NuGet包管理器安装,一般比较常用的上面都有 2.下载exe安装包 在这里https://sourceforge.net/projects/boost/file ...
- Java中的Collections类
转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...
- IIS的部署
https://blog.csdn.net/miner_k/article/details/69388726 https://blog.csdn.net/miner_k/article/details ...
- Android开发实战——记账本(3)
开发日志(3)——适配器 昨天将bean类还有DatabaseHelper类写完.为了在MainActivity中调用,将数据保存到数据库中并显示出来.所以要先编写适配器CostListAdapter ...