leetcode-easy-string- 125 Valid Palindrome

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

参考

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

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]

猜你喜欢

转载自www.cnblogs.com/rosyYY/p/10996358.html
今日推荐