I, entitled Description
Longest Palindromic Substring, find the longest string in the palindrome.
Difficuty是Medium
Second, I realized
After the previous four questions, I'm more and more "perfect" for the consideration of the border.
Submitted a total of five times:
1st and 2nd: Wrong Answer
Mainly "cbbd" mistake, there is some small problem on repeated decision logic
第3、4次: Time Limit Exceeded
I have no problem running native code, but after submitting an error. He gave a long use case:
321012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210123210012321001232100123210123
I run locally is no problem, but not after submission.
I will continue to optimize the code, finally submitted the fifth right. The code is submitted to the following properties:
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.
The complete code is as follows
#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;
}