要注意两点:
1.判断是否是大小写字母或数字,统一将大小写字母变成小写字母
2.isalnum()函数判断是大小写字母或者数字,lower()函数将大写字母变成小写字
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
new_s = [] #这里如果以字符串保存会超时,只好使用数组
for c in s:
if c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z' or c >= '0' and c <= '9':
#if c.isalnum():
new_s.append(c.lower())
length = len(new_s)
for i in range(0, length/2):
if new_s[i] != new_s[length-i-1]:
return False
return True