• 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))
    
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    易购商城首页
    使用HTML5验证博客园用户注册页面
    正则表达式相关内容和用法
    表单
    jQuery制作图片提示效果
    jQuery遍历
    用js制作论坛发贴
    使用jQuery操作DOM
    京东常见问题分类页面
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14252790.html
Copyright © 2020-2023  润新知