Valid Palindrome

Valid Palindrome_第1张图片

code:

<span style="font-size:18px;">class Solution:
    # @param s, a string
    # @return a boolean
    def isPalindrome(self, s):
        zheng = ''
        fan = ''
        for i in s:
            if (i >= 'a' and i <= 'z') or (i >= 'A' and i <= 'Z') or (i >= '0' and i <= '9'):
                zheng+= i
        zheng = zheng.lower()
        for i in range(len(zheng) - 1, -1,-1):
            fan+=zheng[i]

        for i in range(len(zheng)):
            if zheng[i] != fan[i]:
                return False

        return True</span>
在别处看到一简短代码: http://www.cnblogs.com/asrman/p/3974114.html

<span style="font-size:18px;">class Solution:
    # @param s, a string
    # @return a boolean
    def isPalindrome(self, s):
        newS= [i.lower() for i in s if i.isalnum()]
        #return newS == newS[::-1]
        return newS[:len(newS)/2] == newS[(len(newS)+1)/2:][::-1]</span>



你可能感兴趣的:(LeetCode,python)