zoukankan      html  css  js  c++  java
  • 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))
    
  • 相关阅读:
    python05-循环
    python03-列表
    python03 input
    python02-灭霸的选择
    python学习小记01--萌新的进化
    Linux—-软件安装
    linux-认识与分析日志
    Esxi遇到问题汇总。
    xx
    Pramp mock interview (4th practice): Matrix Spiral Print
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14252790.html
Copyright © 2011-2022 走看看