defreverse(x:int):# this use the stringif-10< x <10:return x
str_x =str(x)if str_x[0]!='-':
str_x = str_x[::-1]
x =int(str_x)else:
str_x = str_x[:0:-1]
x =int(str_x)
x =-x
return x if-2147483648< x <2147483647else0
a1 =-123
B1 = reverse(a1)print(B1)#-321defreverse1(x:int):#this use the 位运算符
y, result =abs(x),0# 则其数值范围为 [−2^31, 2^31 − 1]
boundry =(1<<31)-1if x >0else1<<31#1<<31 = 2^31while y !=0:
result = result *10+ y %10# if y = 123, then y%=10, the result is 3if result > boundry:return0
y //=10# if y = 123, then y//=10, the result is 12return result if x >0else-res
a =1<<31print(a)# 2147483648
a1 =-123
B = reverse1(a)#0print(B)