寻找最长回文字符串

class Solution {
    public String longestPalindrome(String s) {
         if (s == null || s.length() == 0) {
            return "";
        }
        int length = s.length();    
        int max = 0;
        String reslut = "";

       for(int i = 1; i <= 2 * length - 1; i++){
            int count = 1;
            while(i - count >= 0 && i + count <= 2 * length  && get(s, i - count) == get(s, i + count)){
                count++;
            }
            count--; // there will be one extra count for the outbound #
            if(count > max) {
                result = s.substring((i - count) / 2, (i + count) / 2);
                max = count;
            }
        }
        return result;
    }

    public char get(String s, int cut){
        if (cut % 2 == 0){
            return '#';
        }
        return s.charAt(cut/2);
    }
}

猜你喜欢

转载自blog.csdn.net/banbanbanzhuan/article/details/78560612