5. 最长回文子串

题目地址:https://leetcode-cn.com/problems/longest-palindromic-substring/
执行用时:304 ms, 在所有 C++ 提交中击败了51.32% 的用户
内存消耗:7 MB, 在所有 C++ 提交中击败了83.19% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
string longestPalindrome(string s) {
int length = s.length();
string maxs = "";
if(s.empty()||s.size()<2){
return s;
} else {
int end,left,right,i;
for (i = 0; i < length; ++i) {
end = length - 1;
right = end;
left = i;
while (right > left){
if (s[right] == s[left]){
--right;
++left;
}else{
end--;
right = end;
left = i;
}
}
if ((end-i+1) > maxs.size()){
maxs = s.substr(i,end-i+1);
}
}
}
return maxs;
}
};

5. 最长回文子串
https://pisces34.github.io/2021/06/09/leetcode/longest/
发布于
2021年6月9日
许可协议