[LeetCode]题解(python):125-Valid Palindrome

题目来源:

  https://leetcode.com/problems/valid-palindrome/


 

题意分析:

  给定一个字符串,只保留字符,并将其转化成小写字母,判断他是否回文字符串。


 

题目思路:

  首先要判断是否字符,.isalnum(),然后就直接是判断是否回文的问题了。


 

代码(python):

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        i,j = 0,len(s)-1
        while i < j:
            while (not s[i].isalnum()) and j > i:
                i += 1
            while (not s[j].isalnum()) and j > i:
                j -= 1
            if s[i].lower() != s[j].lower():
                return False
            i += 1;j -= 1
        return True
            
View Code

 

posted @ 2016-03-22 14:25  Ry_Chen  阅读(238)  评论(0编辑  收藏  举报