[LeetCode] Longest Palindromic Substring,longestsubstring

來源:互聯網
上載者:User

[LeetCode] Longest Palindromic Substring,longestsubstring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

解題思路

網上有一種思路是說先得到輸入字串的反轉字串,然後用動態規劃得到這兩個字串的最長子串即可,但是實際上這種方式有問題。例如,對於輸入“abcdefdcba”,它會得到一個錯誤的迴文子串“abcd”。
一種正確的思路是,用一個二維數組記錄每兩個下標之間是否為迴文子串。下標i從字串最後一個結點開始向前移動,下標j從i開始向後移動,如果下標i和j所指向的字元相等, 而且j-i<=2或者[i+1][j-1]之間的子串為迴文子串,則[i][j]之間的子串為迴文子串。

實現代碼

一種存在問題的代碼:

const int LEN = 1001;int c[LEN][LEN];class Solution {public:    string longestPalindrome(string s)    {        memset(c, 0, sizeof(c));        string s2 = "";        int i = s.size() - 1;        while (i >= 0)        {            s2 += s[i--];        }        int maxLen = 0;        int maxI = 0;        for (i = 1; i <= s.size(); i++)        {            for (int j = 1; j <= s2.size(); j++)            {                if (s[i-1] == s2[j-1])                {                    c[i][j] = c[i-1][j-1] + 1;                    if (c[i][j] > maxLen)                    {                        maxLen = c[i][j];                        maxI = i - 1;                    }                }            }        }        return s.substr(maxI - maxLen + 1, maxLen);    }};

正確代碼(C++):

// Runtime: 260 msconst int LEN = 1000;bool c[LEN][LEN];class Solution {public:    string longestPalindrome(string s)    {        memset(c, 0, sizeof(c));        int maxLen = 0;        string temp;        for (int i = s.size() - 1; i >= 0; i--)        {            for (int j = i; j < s.size(); j++)            {                if (s[i] == s[j] && (j - i <= 2 || c[i+1][j-1]))                {                    c[i][j] = true;                    if (j - i + 1 > maxLen)                    {                        maxLen = j - i + 1;                        temp = s.substr(i, j - i + 1);                    }                }            }        }        return temp;    }};

Java:

// Runtime: 460 mspublic class Solution {    public String longestPalindrome(String s) {        boolean b[][] = new boolean[s.length()][s.length()];        int maxLen = 0;        String temp = "";        for (int i = s.length() - 1; i >= 0; i--){            for (int j = i; j < s.length(); j++){                if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || b[i+1][j-1])){                    b[i][j] = true;                    if (j - i + 1 > maxLen){                        maxLen = j - i + 1;                        temp = s.substring(i, j + 1);                    }                }            }        }        return temp;    }}

Python版本如下,但是寫出來逾時了,暫時沒有找到其他好辦法,請各位指教。

class Solution:    # @param {string} s    # @return {string}    def longestPalindrome(self, s):        b = [[False] * len(s) for i in range(len(s))]        result = ''        maxLen = 0        for i in range(len(s), 0, -1):            for j in range(i, len(s)):                if s[i] == s[j] and (j - i <= 2 or b[i+1][j-1]):                    b[i][j] = True                    if j - i + 1 > maxLen:                        maxLen = j - i + 1                        result = s[i:j+1]        return result

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.