Python solution with O(1) space


  • 0
    L
    import math
    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x < 0:  return False
            if x < 10: return True
            if x < 100:
                return True if x%10 == x//10 else False
            digits = math.log10(x)
            digits = int(math.ceil(digits))+1 if digits == math.ceil(digits) else int(math.ceil(digits))
            for i in xrange(digits/2):
                if x % 10 != (x // 10**(digits-1)) % 10: return False
                x /= 10
                digits -= 2
            
            return True

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.