Solution in Python using str


  • 0
    T
    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            if x < 0:
                sign = -1
                reverse_str = str(x)[:0:-1]  // skipping the negative sign (decrement until index 0)
            else:
                sign = 1
                reverse_str = str(x)[::-1]
    
            reverse_int = int(reverse_str) * sign
            if reverse_int < -2 ** 16 or reverse_int > 2 ** 16 - 1:  // the range for signed int32
                return 0
            return reverse_int
    

Log in to reply
 

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