您的位置:首页 > 编程语言 > Python开发

LeetCode Valid Palindrome Python

2015-03-28 19:02 323 查看

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

class Solution:
# @param s, a string
# @return a boolean
def isPalindrome(self, s):
ls = [i.lower() for i in s if i.isalnum()]
l = len(ls)
tmp1 = ls[:l/2]
tmp2 = list(reversed(ls[l/2:] if l%2==0 else ls[l/2+1:]))

return True if tmp1==tmp2 else False
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: