In [5]:
class Solution(object):
def reverse(self, t):
"""
:type x: int
:rtype: int
"""
ret = ""
is_neg = False
if (-t) > 0:
is_neg = True
t = -t
# 2. Extract each trailing digit and remove it
while t > 1:
r = (t % 10)
t = t / 10
if ret == 0:
continue
ret = ret + chr(48+int(r))
if int(ret) > 2**31:
return 0
# 3. If needed, add the negative sign
if is_neg:
ret = chr(45) + ret
return int(ret)
print (Solution().reverse(-1003))
In [ ]: