题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:
输入: “race a car”
输出: false
思路分析
思想很简单,将字符串转换为list,然后验证倒序和正序是否一致
代码实现
class Solution:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
clean_list = []
for ch in s.lower():
if ch.isalnum(): #如果是字母或者数字则返回True
clean_list.append(ch)
return clean_list == clean_list[::-1]