mycode   9.62%

class Solution(object): 
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        res = ''
        s = s.lower()
        alphanum = string.ascii_lowercase + string.digits
        for i in s:
            if i in alphanum:
                res += i
        return res == res[::-1]

 

注意以下陷阱

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        res = s = [i for i in s if i != ' ']
        print(s)
        res.reverse()
        print(s)
        print(res)
        return res == s

leetcode-easy-string- 125 Valid Palindrome

 

参考

主要是如何简单的判断是否为字符数组

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s="".join(e for e in s if e.isalnum()).lower()
        return s == s[::-1]

 

相关文章:

  • 2021-10-15
  • 2021-08-23
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
猜你喜欢
  • 2021-12-28
  • 2021-11-30
  • 2021-09-20
  • 2021-09-13
  • 2021-09-27
  • 2021-07-22
相关资源
相似解决方案