给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。 如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。 假设环境不允许存储 64 位整数(有符号或无符号)
s = 0
t = 1
x = -123
if x < 0 :
t = -1
x = -x
for i in range(len(str(x))-1,-1,-1):
s = s + int(str(x)[i]) * 10 ** (i)
if s > 2**31 - 1 or s < -2**31:
s = 0
print(s * t)
这不是最优算法,详细请参考力扣 https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/



