首页 > 其他 > 详细

LeetCode #Valid Palindrome#

时间:2015-04-07 17:48:24      阅读:123      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!