Solution in Python


  • 0
    M
    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            digits = len(str(abs(x)))
            if digits==1:
                res = x
                return res
            else:
                mock = abs(x)
                res = 0
                for i in range(digits-1, -1, -1):
                    n = mock // (10**(i))
                    mock -= n*(10**(i))
                    res += n*(10**(digits-1-i))
                if res >= (2**31+1):
                    return 0
                if x < 0:
                    res = 0 - res
                return res
                   
    

Log in to reply
 

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