题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
思路:分文两种情况,一种是以一个数为中心向两边扩散,另一种中间两个数相等的情况向两边扩散
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
public int getLongestPalindrome (String A) {
// write code here
if(A.length()<2){
return A.length();
}
if(A.length()==2){
if (A.charAt(0)==A.charAt(1)){
return 2;
}
return 1;
}
int left = 0;
int right = 0;
int max = 1;
//奇数
for(int i = 0;i <A.length();i++){
left = i-1;right=i+1;
int temp = 1;
while(left>-1&&right<A.length()&&A.charAt(left)==A.charAt(right)){
temp+=2;
left--;
right++;
}
max=max<temp?temp:max;
}
//偶数
for(int i = 0;i <A.length();i++){
left = i-1;right=i+2;
int temp = 2;
int count = 0; //若一次都进入判断 将temp清空初始值
while(left>-1&&right<A.length()&&A.charAt(left)==A.charAt(right)&&A.charAt(i)==A.charAt(i+1)){
count++;
temp+=2;
max=max<temp?temp:max;
left--;
right++;
}
if (count<1){
temp=1;
}
max=max<temp?temp:max;
}
return max;
}
}

查看15道真题和解析