5.最长回文子串(方法1)

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        a = 1   # a是当前的最大字符长度
        b = 1   # b是最大字符长度
        c = ''  # c是返回的文本
        if len(s) == 1:
            return s
        for i in range(len(s)-1):
            for j in range(i + 1, len(s)):
                if s[j] == s[i]:
                    if len(s[i: j + 1]) % 2 == 0:
                        if len(s) == 2:
                            return s
                        elif len(s[i: j + 1]) == 2:
                            a = 2
                            if a > b:
                                b = a
                                c = s[i: j + 1]
                        else:
                            if len(s) == 4:
                                if s[2] == s[3]:
                                    c = s
                            else:
                                for k in range(i + 1, int(len(s[i: j + 1]) / 2 + i)):
                                    if s[k] == s[j + i - k]:
                                        a = len(s[i: j + 1])
                                    else:
                                        a = ''
                                        break
                                if a != '' and a > b:
                                        b = a
                                        c = s[i: j + 1]
                    else:
                        if len(s) == 3:
                            return s
                        elif len(s[i: j + 1]) == 3:
                            a = 3
                            if a > b:
                                b = a
                                c = s[i: j + 1]
                        else:
                            if len(s[i: j + 1]) % 2 != 0:
                                for k in range(i + 1, int((j + i) / 2)):
                                    if s[k] == s[2 * int((j + i) / 2) - k]:
                                        a = len(s[i:j + 1])
                                    else:
                                        a = ''
                                        break
                                if a != '' and a > b:
                                        b = a
                                        c = s[i: j + 1]
                                elif i == 0 and j == len(s) - 1 and c == '':
                                    c = s[0]
                            else:
                                for k in range(i + 1, int(j / 2) + 1):
                                    if s[k] == s[j - i]:
                                        a = len(s[i: j])
                                        if a > b:
                                            b = a
                                            c = s[i: j + 1]
                                    else:
                                        break
                elif i == 0 and j == len(s) - 1 and c == '':
                    c = s[0]
        return c


print(Solution.longestPalindrome(0, 'eeeeeeeee'))

你可能感兴趣的:(5.最长回文子串(方法1))