Python算法題----Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.


class Solution(object):
    def numLen(self, n):
        i = 1
        while True:
            n /= 10
            if n > 0:
                i += 1
            else:
                break
        return i


    def pow(self, n):
        i = 1
        r = 1
        while i < n:
            r *= 10
            i += 1
        return r
    
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        if x < 10:
            return True
        n = x
        nlen = self.numLen(n)
        t = self.pow(nlen)
        i = 0
        while i < (nlen / 2):
            if (x / t) % 10 != n % 10:
                return False
            n /= 10
            t /= 10
            i += 1
        return True


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章