Reverse Integer
Given a signed 32-bit integer x
, return x
with its digits reversed. If reversing x
causes the value to go outside the signed 32-bit integer range [-2<sup>31</sup>, 2<sup>31</sup> - 1]
, then return 0
.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
isNegative=False
if x<0:
x=x*-1
isNegative=True
x_str=list(str(x))
num=0
for i in range(len(x_str)):
base = 10**i
num+=base*int(x_str[i])
if num < 2**31:
if isNegative:
return num*-1
return num
return 0