Datawhale-LeetCode集训打卡-最长回文子串

Datawhale-LeetCode集训打卡-最长回文子串

  • Datawhale-LeetCode集训打卡-第三天-最长回文子串(Longest Palindromic Substring)
    • Solution

Datawhale-LeetCode集训打卡-第三天-最长回文子串(Longest Palindromic Substring)

Solution

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

你可能感兴趣的:(算法,LeetCode,Datawhale)