leetcode最长回文子串

Posted erdanyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode最长回文子串相关的知识,希望对你有一定的参考价值。

最长回文子串

解题思想:动态规划

class Solution 
    public String longestPalindrome(String s) 
        int len = s.length();
        boolean[][] b = new boolean[len][len];
        int max = 0;
        int begin = 0;
        for(int i=0;i<len;i++)
            for(int j=0;j<=i;j++)
                if(s.charAt(i)==s.charAt(j)&&(i-j<=2 || b[j+1][i-1]))
                    b[j][i] = true;
                    if(i-j+1>max)
                        max = i - j + 1;
                        begin = j;
                    
                
            
        
        return s.substring(begin,begin+max);
    

 

以上是关于leetcode最长回文子串的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 最长回文子串

Leetcode 5. 最长回文子串

Leetcode最长回文子串

[LeetCode] 最长回文子串

LeetCode 5 最长回文子串

LeetCode:最长回文子串