类型:字符串
-
- 验证回文串 💚
https://leetcode-cn.com/problems/valid-palindrome/
❓ 判断一个字符串是否为回文串。只关注字符串中的字母和数字字符,且不区分字母的大小写。
💡 双指针
双指针从两端向中间遍历,并且跳过非字母和数字的字符。
class Solution: def isPalindrome(self, s: str) -> bool: i = 0 j = len(s) - 1 while i < j: while i < j and not s[i].isalpha() and not s[i].isdigit(): i += 1 while i < j and not s[j].isalpha() and not s[j].isdigit(): j -= 1 if s[i].lower() != s[j].lower(): return False else: i += 1 j -= 1 return True时间复杂度:O(n),空间复杂度:O(1)