class Solution {
public String longestPalindrome(String s) {
if(s.length()<=1)
return s;
String zz = "";
int ll = 0;
int len = s.length();
//0 1 2 3 4 5
//b , a , b , b , a , b
//0 1 2 3 4 5 6 7 8 9 10
for(int i=0;i<=2*len-2;i++){
int tmp = (i%2==0)?1:0;
for(int j=i/2;j>=0&&i-j<len;j--){
if(s.charAt(j)==s.charAt(i-j)){
tmp = (j==i-j)?tmp:tmp+2;
if(tmp>ll){
ll = tmp;
zz = s.substring(j, i-j+1);
}
}else{
break;
}
}
}
return zz;
}
}
网友评论