题目
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转,地址。
示例:
输入: 123
输出: 321
解题思路
- python3取模跟c++不同,python3是向下取整: − 21 % 10 = 9 -21\%10 = 9 −21%10=9,所以要先转化成整数再取整。
- 注意边界值溢出问题
Python实现
class Solution:
def reverse(self, x: int) -> int:
t = []
res = 0
y = x
y = abs(y)
while(y != 0):
res = res * 10 + y % 10
y = int(y / 10)
# MAX_VALUE : 2147483647 MIN_VALUE : -2147483648
if res > 2147483647 or -res < -2147483648:
return 0
elif x > 0:
return res
else:
return -res
933

被折叠的 条评论
为什么被折叠?



