码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode #Valid Palindrome#

时间:2015-04-07 17:49:59      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

LeetCode #Valid Palindrome#




技术分享


我的Python解答:


"""
Programmer  :   EOF
e-mail      :   jasonleaster@gmail.com
Date        :   2015.04.07
File        :   vp.py
"""

import string

class Solution:

    def isPalindrome(self, s):
        length = len(s)
        left    = 0
        right   = length - 1
        while left < right:

            if self.isCharacter(s[left]) is False:
                left += 1
                continue

            if self.isCharacter(s[right]) is False:
                right -= 1
                continue

            if string.lower(s[left]) != string.lower(s[right]):
                return False

            left  +=  1
            right -=  1

        return True

    def isCharacter(self, c):
        if c is None:
            return False

        if (c <= 'z' and c >= 'a') or            (c <= 'Z' and c >= 'A') or            (c <= '9' and c >= '0'):
            return True

        return False

#---------------- just for testing --------------

s = Solution()
string_1 = "abcba"
print string_1
if s.isPalindrome(string_1) :
    print "is Palindrome"

string_1 = "ab"
print string_1
if s.isPalindrome(string_1) :
    print "is Palindrome"

string_1 = "1a2"
print string_1
if s.isPalindrome(string_1) :
    print "is Palindrome"



技术分享


LeetCode #Valid Palindrome#

标签:

原文地址:http://blog.csdn.net/cinmyheart/article/details/44921063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!