题解 | 反转数字
反转数字
https://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param x int整型
# @return int整型
#
class Solution:
def reverse(self , x: int) -> int:
# write code here
if x==0:#对0进行处理
x = 0
elif x>0:#对正数进行处理
x = int(str(x)[::-1])
else:#对负数进行处理
x = int('-'+str(x)[1:][::-1])
if x>2**31-1 or x<-2**31:#检查是否超出范围
return 0
else:
return x

