题解:定义一个判断回文的函数,然后递归,也是用左右两个指针,同167 和 633
class Solution(object):
def validPalindrome(self, s: str) -> bool:
def checkPalindrome(i, j):
while i < j:
if s[i]!= s[j]:return False
i+=1
j-=1
return True
low=0
high =len(s)-1
while low < high:
if s[low] == s[high]:
low += 1
high-=1
else:
return checkPalindrome(low+1,high) or checkPalindrome(low,high-1)
#要么删掉左边的一个元素,要么删掉右边的一个元素
return True
测试: