Given a 32-bit signed integer, output the integer backwards


Ex. :

Input: 123

Output: 321


Input: – 123

Output: – 321


Input: 120

Output: 21


Solution:

class Solution:
    def reverse(self, x):
        x = str(x)[::-1]
        x = -int(x[:-1]) if x[-1] == The '-' else int(x)
        
        limit = pow(2, 31)
        if abs(x) > limit:
            return 0
        return xCopy the code