Reverse Integer Soln in Python3


  • 0
    D
    class Solution:
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            returned_val = 0
            if x >= 0:
                digits = [int(d) for d in str(x)]
                reverse = list(reversed(digits))
                returned_val = int(''.join(str(alpha) for alpha in reverse))
                if returned_val > 2147483647:
                    return 0
                else:
                    return returned_val
            else:
                unassigned = abs(x)
                digits = [int(d) for d in str(unassigned)]
                reverse = list(reversed(digits))
                returned_val = -1*(int(''.join(str(alpha) for alpha in reverse)))
                if returned_val < -2147483647:
                    return 0
                else:
                    return returned_val
    

Log in to reply
 

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