给定一个 32 位有符号整数,将整数中的数字进行反转。
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
flag = 2**31
if x > 0 and x <= flag-1:
reverse_num = int(str(x)[::-1])
return reverse_num if reverse_num <= flag-1 else 0
elif x < 0 and x >= -flag:
reverse_num = -int(str(-x)[::-1])
return reverse_num if reverse_num >= -flag else 0
else:
return 0
![image.png](https://upload-images.jianshu.io/upload_images/4200209-48fee222b6dd9737.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)