首页 > 编程语言 > 详细

LeetCode Valid Palindrome Python

时间:2015-03-28 20:20:38      阅读:250      评论:0      收藏:0      [点我收藏+]

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

 

LeetCode Valid Palindrome Python

原文:http://www.cnblogs.com/fyymonica/p/4374634.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!