125. 验证回文串

  1. 题目链接

  2. 解题思路:双指针,一个一个对比,跳过非数字字母,对比时,忽略大小写

  3. 代码

    
    
    class Solution:
        def isPalindrome(self, s: str) -> bool:
            # 双指针
            i, j = 0, 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 s[i].lower() != s[j].lower():
                    return False
                i += 1
                j -= 1
            return True
    
posted @ 2024-12-26 16:19  ouyangxx  阅读(15)  评论(0)    收藏  举报