[LeetCode]125. Valid Palindrome

125. Valid Palindrome

只要字母和数字,再判断是否是回文即可。

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if not s:
            return True
        strs = ''.join(filter(lambda x:x.isalnum(), s.lower()))
        left, right = 0, len(strs)-1
        while left <= right:
            if strs[left] != strs[right]:
                return False
            left += 1
            right -= 1
        return True

可以写的更简单:

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        string = filter(str.isalnum, str(s)).lower()
        return  string == string[::-1]
posted @ 2017-08-24 01:36  banananana  阅读(75)  评论(0编辑  收藏  举报