9. Palindrome Number Leetcode Python 2016 new Season

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

click to show spoilers.

Show Tags
Show Similar Problems





The problem requirement doesn't make sense, we still need constant space to do this problem. the time complexity is O(log(n)).
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        tmp_x = x
        residual = 0
        while x >= 1:
            rest = x % 10
            residual = residual * 10 + rest
            x /= 10
        if tmp_x == residual:
            return True
        return False

 

你可能感兴趣的:(9. Palindrome Number Leetcode Python 2016 new Season)