public String longestPalindrome(String s) {
String rs = "";
int res = 0;
for(int i = 0; i< s.length(); i++){
int left = i, right = i;
while(left >= 0 && s.charAt(i) == s.charAt(left)) left --;
while(right < s.length() && s.charAt(i) == s.charAt(right)) right ++;
while (left >= 0 && right < s.length()){
if (s.charAt(left) != s.charAt(right))
break;
left --; right ++ ;
}
if(right - left - 1 > res){
res = right - left - 1;
rs = s.substring(left+1, right);
}
}
return rs;
}
原文地址:https://www.cnblogs.com/whyaza/p/10661298.html
时间: 2024-11-02 05:50:25