望着时间滴答滴答的流过,我不曾改变过 . . .

leetcode 5. Longest Palindromic Substring [java]

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;
    }
posted @ 2019-04-06 11:57  whyaza  阅读(76)  评论(0编辑  收藏  举报