Problem description:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
class Solution(object):
def reverse(self, x):
k = 2**31-1
if x >= 0:
b = str(x)
b1 = int(b[::-1])
if b1 > k:
return 0
elif b1 < -k:
return 0
else:
return int(b1)
elif x < 0:
c = str(x)
d = c[1:len(c)]
e = d[::-1]
f = c[0] + e
f1 = int(f)
if f1 > k:
return 0
elif f1 < -k:
return 0
else:
return f1
"""
:type x: int
:rtype: int
"""
becsause 32-bit signed integer,so k=2**31-1