Reverse Integer in Python


  • 0
    J

    class Solution(object):
    def reverse(self, x):
    """
    :type x: int
    :rtype: int
    """
    sign = 1 if x > 0 else -1
    x = abs(x)

        y = 0
        while x > 0:
            y = y * 10 + x % 10
            x = x / 10
        
        if abs(sign * y) > (2 ** 31):
            return 0
        
        return sign * y

Log in to reply
 

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