首页 > 编程语言 > 详细

Python算法题----Valid Palindrome

时间:2016-01-20 15:47:53      阅读:190      评论:0      收藏:0      [点我收藏+]

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.

class Solution(object):
    def check(self, s):
        if s.isdigit() or s.isalpha():
            return True
        return False
    
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower()
        if not s or not s.strip():
            return True
        i, j = 0, len(s) - 1
        while i < j:
            while i < j and not self.check(s[i]):
                i += 1
            while i < j and not self.check(s[j]):
                j -= 1
            if s[j] != s[i]:
                return False
            i += 1
            j -= 1
        return True


本文出自 “烛影摇红” 博客,请务必保留此出处http://gccmx.blog.51cto.com/479381/1736774

Python算法题----Valid Palindrome

原文:http://gccmx.blog.51cto.com/479381/1736774

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