代码
class Solution:
### 0209 逆序求解(52 ms,14.9 MB)
def reverse(self, x: int) -> int:
# 首先对x求绝对值
res, y = 0, abs(x)
# 注意:bound也取绝对值,不分正负
bound = 2 ** 31 if x < 0 else 2 ** 31 - 1
# 当y中的位数还有时,进行循环
while y != 0:
res = res * 10 + y % 10
if res > bound: return 0 # 若当前数越界,则直接返回0
y //= 10
return res if x > 0 else -res # 正负性由原始的x确定