[LeetCode]Longest Palindromic Substring

public class Solution {
    public String longestPalindrome(String s) {
        int length = s.length();
        String result = "";
        for (int i = 0; i < length; i++) {
            int left = i;
            int right = i;
            while (right + 1 < length && s.charAt(right) == s.charAt(right + 1)) {
                right ++;
            }
            while (left > 0 && right + 1 < length && s.charAt(left - 1) == s.charAt(right + 1)) {
                left --;
                right ++;
            }
            result = right - left + 1 > result.length() ? s.substring(left, right + 1) : result;
        }
        return result;
    }
}

 

posted @ 2015-12-06 09:47  Weizheng_Love_Coding  阅读(117)  评论(0编辑  收藏  举报