Python 给定一个字符串s,找到最长的回文子字符串。Can';我不明白为什么代码不';我不能回答正确的问题

Python 给定一个字符串s,找到最长的回文子字符串。Can';我不明白为什么代码不';我不能回答正确的问题,python,Python,我不明白为什么max函数在最后一次迭代中选择了两个字符串中较小的一个。LeetCode出现问题:max(ans,current)按字典顺序返回最新的字符串,而不是最长的字符串 您可以使用max(ans,current,key=len)。也就是说,基于给定函数,len的两个位置参数的最大值 max(ans,current)按字典顺序返回最新的字符串,而不是最长的字符串。非常感谢! class Solution(object): def longestPalindrome(self, s):

我不明白为什么max函数在最后一次迭代中选择了两个字符串中较小的一个。LeetCode出现问题:

max(ans,current)
按字典顺序返回最新的字符串,而不是最长的字符串


您可以使用
max(ans,current,key=len)
。也就是说,基于给定函数,
len
的两个位置参数的最大值

max(ans,current)
按字典顺序返回最新的字符串,而不是最长的字符串。非常感谢!
class Solution(object):
    def longestPalindrome(self, s):
        current = ""
        ans = ""

        for n in range(len(s)):
            for index in range(n,len(s)):
                current += s[index]
                print(current)
                if self.ispalindrome(current):
                    print(len(ans),len(current))
                    ans = max(ans,current)
            current = ""
        return ans

    def ispalindrome(self, s):
        return s == s[::-1]