本来觉得是一个进栈出栈的问题,后来想想,反转一下就行了,这样也不用考虑奇数偶数。而且用Python来写感觉就像是作弊一样,直接有转换成大小写的方法了。
class Solution:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
#去掉不和规定的并且全部转换成小写
for st in s:
if st>='0' and st<='9':stack.append(st)
if st<='z' and st>='a':stack.append(st)
if st<='Z' and st>='A':stack.append(st.lower())
flag = True
for i in range(len(stack) // 2):
if stack[i] != stack[len(stack)-i-1]:
flag = False
break
return flag