【leetcode】5. Longest Palindromic Substring(Python & C++)

来源:互联网 发布:生化危机游戏 知乎 编辑:程序博客网 时间:2024/06/05 03:28

5. Longest Palindromic Substring

题目链接

5.1 题目描述:

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: “babad”

Output: “bab”

Note: “aba” is also a valid answer.

Example:

Input: “cbbd”

Output: “bb”

5.2 解题思路:

  1. 思路一:首先创建判断是否是回文的函数ishuiwen。需要三个参数,string s,int x,int y。x和y是判断字符串s的起始位置。如果x==y,则为奇数回文字符串判断,并且回文字符串起始长度设为1,x–,y++。如果x!=y,则为偶数回文字符串判断,并且回文字符串起始长度设为0。然后循环条件,x大于等于0 并且 y小于s.length() && s[x]==s[y],则长度+=2,x–,y++。这样最后返回得到的回文字符串s.substr(x + 1, count)。然后在longestPalindrome函数中,寻找最大的回文子串。从k=0开始,每次判断两个回文子串,一个奇数的,一个偶数的,并记录长度大的子串。

  2. 思路二:首先,设置回文子串起始位置first=0,长度count=1。for k循环遍历字符串s,首先设置临时长度c_temp=0。用i=k,然后for j逆向找个s中第一个与是s[i]相等的字符,这是回文子串的结束位置。此时回文子串的临时长度c_temp=2,然后i++,j–。如果i小于j,则继续判断s[i]==s[j],相等则i++,j–,c_temp+=2,不相等则c_temp=0,同时break。如果i==j,则c_temp+=1.然后判断c_temp与count的大小,决定是否更新count与first。这样直至循环结束。最后,返回s.substr(first,count)。

5.3 C++代码:

1、思路一代码(32ms):

class Solution {public:    string ishuiwen(string s,int x,int y)    {        int count;        if (x == y)        {            count = 1;            x--;            y++;        }                       else            count = 0;          while (x>=0 && y<s.length() && s[x]==s[y] )        {            x--;            y++;            count += 2;        }        return s.substr(x + 1, count);    }    string longestPalindrome(string s) {        if (s.length() == 0)            return "";        if (s.length() == 1)            return s;        string sub = s.substr(0, 1);        for (int k = 0; k < s.length()-1;k++)        {            string s1 = ishuiwen(s,k,k);            if (s1.length()>sub.length())                sub = s1;            string s2 = ishuiwen(s, k, k+1);            if (s2.length() > sub.length())                sub = s2;        }        return sub;    }};

2、思路二代码(超时):

class Solution94 {//超时public:    string longestPalindrome(string s) {            if (s.length() == 0)            return "";        if (s.length() == 1)            return s;        int first = 0;        int count = 1;        for (int k = 0; k < s.length(); k++)        {                   int c_temp = 0;            for (int p = s.length() - 1; p > k; p--)            {                   int i = k;                if (s[i] != s[p])                    continue;                int j = p;                c_temp = 2;                i++;                j--;                while (i < j)                {                    if (s[i] != s[j])                    {                        c_temp = 0;                        break;                    }                    else                    {                        i++;                        j--;                        c_temp += 2;                    }                }                if (i == j)                {                    c_temp += 1;                }                   if (c_temp > count)                {                    count = c_temp;                    first = k;                }            }           }        return s.substr(first,count);    }};

5.4 Python代码:

1、思路一代码(1352ms):

class Solution(object):    def longestPalindrome(self, s):        """        :type s: str        :rtype: str        """        if len(s)==0 or len(s)==1:            return s        sub=s[0:1]        for i in range(len(s)-1):            def ishuiwen(s,x,y):                while x>=0 and y<len(s) and s[x]==s[y]:                    x-=1                    y+=1                return s[x+1:y]            s1=ishuiwen(s,i,i)            if len(s1)>len(sub):                sub=s1            s2=ishuiwen(s,i,i+1)            if len(s2)>len(sub):                sub=s2        return sub

原创粉丝点击