题目
方法一:使用python内置函数将字符串中的标点符号、空格删除,并转为小写字母;使用列表切片将字符串reverse,判断是否相等
class Solution:
def isPalindrome(self, s: str) -> bool:
import string
a=s.translate(str.maketrans('', '', string.punctuation))
s="".join(a.split()).lower()
return s==s[::-1]
方法二:双指针
class Solution:
def isPalindrome(self, s: str) -> bool:
import string
a=s.translate(str.maketrans('', '', string.punctuation))
s="".join(a.split()).lower()
l,h=0,len(s)-1
while l<h:
if s[l]!=s[h]:
return False
l+=1
h-=1
return True