mycode   12.51%

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        length = len(s)
        if length==0:
            return ''
        res = s[0]
        for i in range(length-1):
            for j in range(i+1,length):
                if s[j] == s[i]:
                    temp = s[i:j+1]   
                    if temp == temp[::-1]:
                        if len(temp) > len(res):
                            res = temp[:]
        return res

 

参考:

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        n = len(s)
        maxl, start = 0, 0
        for i in range(n):
            if i - maxl >= 1 and s[i-maxl-1:i+1] == s[i-maxl-1:i+1][::-1]:
                start = i - maxl - 1
                maxl += 2
                continue
            if i - maxl >= 0 and s[i-maxl:i+1] == s[i-maxl:i+1][::-1]:
                start = i - maxl
                maxl += 1
        return s[start: start + maxl]