题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
int getLongestPalindrome(char* A ) {
// write code here
int max=1,cnt=0;//max=1是为单字符设计的
int i=0,j=0;//i是中心,j是偏移量
while(A[i] != '\0'){
//aba情况
j=1;
while(A[i+j] == A[i-j]){
cnt = j*2+1;
if(max < cnt) max = cnt;
j++;
}
//abba情况
j=1;
while(A[i+j] == A[i+1-j]){
cnt = j*2;
if(max < cnt) max = cnt;
j++;
}
i++;
}
return max;
}

查看12道真题和解析