1、转成字符串
class Solution:
def reverse(self, x: int) -> int:
x_str = str(x)
x_len = len(x_str)
x_re = x_str[::-1]
try:
x_re = int(x_re)
except:
x_re = -int(x_re.split('-')[0])
if x ==0 or x_re < -2**31 or x_re > 2**31 -1:
return 0
return x_re
2、整数直接计算
class Solution:
def reverse(self, x: int) -> int:
z_x = 0
i=0
if x < 0:
i = 1
x = abs(x)
while x!=0 :
x_y = x%10
x = x//10
z_x = z_x*10 + x_y
if i ==1:
z_x = -z_x
if z_x ==0 or z_x < -2 **31 or z_x>2**31-1:
z_x = 0
return z_x