This solution beats 75% of python submissions


  • 2
    S

    This solution beats 75% of python submissions.

    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
    
        # if number is between -9 to +9, reversing will yield same value
        if abs(x) < 10:
            return x
        
        # save the sign
        sign = 1 if x >= 0 else -1
        # make x positive. If it is negative, muliplying with -1 will give positive.
        x = sign * x
    
        # make number a string
        x = str(x) 
    
        # reverse the string
        x = x[::-1]
    
       #convert the number into  integer and multiply with sign
        x = sign * int(x)
    
        # return x if the integer is not overflow else return 0.
        return 0 if x >= 2147483647 or x <= (-2147483647-1) else x

Log in to reply
 

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