给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000
class Solution {
public String longestPalindrome(String s) {
if(s==null || s.length()<=1)
return s;
int len = s.length();
String longest = String.valueOf(s.charAt(0));
for(int i=0; i<len-1; i++){
String temp = getMax(s, i, i);
if(temp.length() > longest.length())
longest = temp;
temp = getMax(s, i, i+1);
if(temp.length() > longest.length())
longest = temp;
}
return longest;
}
String getMax(String s, int left, int right){
while(left>=0 && right<=s.length()-1 && s.charAt(left) == s.charAt(right)){
left--;
right++;
}
return s.substring(left+1, right);
}
}