题解 | 最长回文子串
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
int getLongestPalindrome(string A) {
// write code here
int n=A.size();
if(n==0)
{
return 0;
}
int maxlength=1;
for(int i=0;i<2*n-1;i++)
{
int left=i/2;
int right=left+i%2;
while(left>=0&&right<n&&A[left]==A[right])
{
maxlength=max(maxlength,right-left+1);
left--;
right++;
}
}
return maxlength;
}
};
查看7道真题和解析
美团成长空间 2667人发布