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

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]

 

上一篇:JavaScript中的回文检查器 – 不知道如何调试


下一篇:hdu 三部曲 Cheapest Palindrome