Python solution: beats 100%


  • 0
    W
    import math
    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x<0:
                return False
            if x == 0:
                return True
            n = int(math.log10(x)) + 1 # number of digits
            if n == 1: # 1 digit
                return True
            mask = math.pow(10, n-1)
            
            for _ in xrange(n/2):
                lsb = x%10
                msb = int(x/mask)
                if lsb != msb:
                    return False
                x%=mask
                x = int(x/10)
                mask/=100 # number is shortened by 2 digits
                
            return True
    

Log in to reply
 

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