LeetCode|125—验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:

输入: “race a car”
输出: false

def isPalindrome(self, s: str) -> bool:
	    alpha = 'abcdefghijklmnopqrstuvwxyz0123456789'
	    a = list(s.lower()) # 转为小写
	    c = []
	    for i in a:
	        if i in alpha:
	            c.append(i)
	    if c == c[::-1]:
	        return True
	    else:
	        return False

猜你喜欢

转载自blog.csdn.net/Python_Matlab/article/details/107725033