解题思路
双指针,一个从左往右,另一个从右往左
跳过非字母的字符,对碰到的字符,转化为小写然后比较
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
效果图
网友评论