135-125. 验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。(第二个不是我写的其他的都两个是我写的)
class Solution(object):
    def isPalindrome1(self, s):
        """
        :type s: str
        :rtype: bool
        """
        length = len(s)
        i = 0
        j = length - i - 1
        while i <= j:
            while i <= j and not s[i].isalnum():
                i += 1

            while i <= j and not s[j].isalnum():
                j -= 1

            if i <= j and s[i].lower() != s[j].lower():
                return False
            j -= 1
            i += 1
        return True

    def isPalindrome2(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower()
        result = ''.join(filter(str.isalnum, s)).lower()
        return result == result[::-1]

    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        i = 0
        j = len(s) - 1
        while i < j:
            while i < j and not s[i].isalnum():
                i += 1

            while i < j and not s[j].isalnum():
                j -= 1

            if i < j and s[i].lower() != s[j].lower():
                return False
            j -= 1
            i += 1
        return True


if __name__ == '__main__':
    s = Solution()
    s1 = ".,"
    print(s.isPalindrome(s1))
原文地址:https://www.cnblogs.com/liuzhanghao/p/14252790.html