leetcode 5. Longest Palindromic Substring [java]

Posted why

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }

以上是关于leetcode 5. Longest Palindromic Substring [java]的主要内容,如果未能解决你的问题,请参考以下文章

leetcode--5. Longest Palindromic Substring

#Leetcode# 5. Longest Palindromic Substring

LeetCode题解 #5 Longest Palindromic Substring

[LeetCode] 5 Longest Palindromic Substring

Leetcode #5. Longest Palindromic Substring

LeetCode 5_Longest Palindromic Substring