Straightforward Python implementation: ~54ms


  • -1
    A
        sign = cmp(x, 0)
        p = abs(x)
        result = 0
        
        while p > 0:
            rem = p%10
            result = result*10 + rem
            p /= 10 
         
        # more succinct but less efficient:
        #return sign*result * (result < 2**31)
            
        if result > 2**31: return 0
        result = sign*result
        return result

Log in to reply
 

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