刷题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 ...
随机推荐
- 不同页面获取同一cookie变量值不同的问题及解决方法
在使用cookie时发现不同页面获取到的同一个cookie变量的值不同,本篇博客介绍其中一种情况的解决方法,通过设置path的方法可使得在同一个网站下获取的cookie变量一致. 问题描述 在www. ...
- 使用Opencv3.2出现l error C4996:fopen
------ 已启动生成: 项目: test, 配置: Debug x64 ------1> test.cpp1>e:\vs2015opencv\opencv3.2\opencv\buil ...
- C语言--“.”与“->”有什么区别?
这虽然是个小问题,但有时候很容易让人迷惑,因为有的时候用混淆了,程序编译不通过. 下面说说我对它们的理解. 一般情况下用“.”,只需要声明一个结构体.格式是,结构体类型名+结构体名.然后用结构 ...
- sql 应用记录
SELECT * FROM (select aa.*,bb.mentalvisitid, ' then '家庭访视' else '电话' end as BCSFXS ,bb.visitdate, ' ...
- Python七夕记
- PHP+jPaginate插件制作无刷新分页实例
jPaginate是一款动感滚动分页插件,它的表现形式是像分页的按钮一样,有意思的是这些按钮却可以左右滚动,可以通过单击或 鼠标滑向点两侧的小箭头来控制按钮的左右滚动. 读取第一页数据: <di ...
- 阻止click点击事件
遇到一个屏蔽点击事件,以前一般都是通过js控制,阻止事件,今天看到css加一个样式就能屏蔽,来记录一下 //css禁用鼠标点击事件 .test { pointer-events: none; } 随便 ...
- Java爬虫学习(2)之用对象保存文件demo(1)
package com.mieba.spider; import java.util.ArrayList; import java.util.List; import java.util.Vector ...
- PHP 可选参数
function chooseable($a,$b,$d,$c="我是可选参数c"){ //注意:可选参数一定要是在必选参数后面(有默认值就是可选参数):PHP中参数一定要变量符号 ...
- resize2fs: 报错
报错如下 [root@localhost ~]# resize2fs /dev/mapper/centos-root resize2fs (-Dec-) resize2fs: Bad magic nu ...