2018-08-16 LeetCode 最长回文子串

给定一个字符串 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 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);    
    }  
}

你可能感兴趣的:(2018-08-16 LeetCode 最长回文子串)