Given a 32-bit signed integer, reverse digits of an integer.
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2**31, 2**31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ if (x > 2147483647 and x < -2147483648): return 0 x_list = list(str(x)) if x_list[0] == "-": x_list = x_list[1:] x_list.reverse() if (int(''.join(x_list)) > 2147483647): return 0 return -int(''.join(x_list)) else: x_list.reverse() if (int(''.join(x_list)) > 2147483647): return 0 return int(''.join(x_list))