Python Straight Forward Solution


  • 0
    G
    def reverse(x):
        sign = -1 if x<0 else 1
        res, x = 0, abs(x)
        while x:
            res = res*10 + (x%10)
            x /= 10
        # handle the overflow bound
        if res > 2**31+1 or res < -2**31-1:
            return 0
        return res*sign

  • 0
    N

    I think the overflow bounds need to be changed. The maximum value for a 32bit signed integer is 2^31-1 and the lowest is -2^31.


Log in to reply
 

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