-
解题思路:双指针,一个一个对比,跳过非数字字母,对比时,忽略大小写
-
代码
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