5. Longest Palindromic Substring

Posted ymjyqsx

tags:

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

https://www.cnblogs.com/grandyang/p/4464476.html

用动态规划做

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.empty())
            return "";
        int dp[s.size()][s.size()] = {0};
        int left = 0,right = 0,len = 0;
        for(int i = 0;i < s.size();i++){
            for(int j = 0;j < i;j++){
                dp[j][i] = (s[i] == s[j]) && (i-j <= 2 || dp[j+1][i-1]);
                if(dp[j][i] && len < i - j + 1){
                    left = j;
                    right = i;
                    len = i - j + 1;
                }
            }
        }
        return s.substr(left,right - left + 1);
    }
};

 

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

#Leetcode# 5. Longest Palindromic Substring

5. Longest Palindromic Substring - Unsolved

5. Longest Palindromic Substring

5. Longest Palindromic Substring

[LeetCode] 5 Longest Palindromic Substring

5. Longest Palindromic Substring