Solution in Python3


  • 0
    S
    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            
            if x < 10 and x > -10: return x # single digit
      
            maximum = 2**31 - 1 # largest 32 bit signed int
            
            sign = -1 if x < 0 else 1;
            r = int(str(x*sign)[::-1]) # abs val to string, reverse, and back to int
            
            if r <= maximum:
                return r * sign
            return 0
    

Log in to reply
 

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