美文网首页
python实现leetcode之125. 验证回文串

python实现leetcode之125. 验证回文串

作者: 深圳都这么冷 | 来源:发表于2021-10-06 14:13 被阅读0次

    解题思路

    双指针,一个从左往右,另一个从右往左
    跳过非字母的字符,对碰到的字符,转化为小写然后比较

    125. 验证回文串

    代码

    class Solution(object):
        def isPalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            # s = [c.lower() for c in s if c.isalnum()]
            length = len(s)
            low, high = 0, length-1
            while low < high:
                while not s[low].isalnum() and low < high:
                    low += 1
                while not s[high].isalnum() and low < high:
                    high -= 1
                if s[low].lower() != s[high].lower():
                    return False
                low += 1
                high -= 1
            return True
    
    效果图

    相关文章

      网友评论

          本文标题:python实现leetcode之125. 验证回文串

          本文链接:https://www.haomeiwen.com/subject/noconltx.html