classSolution(object):defreverse(self, x):"""
:type x: int
:rtype: int
"""
sign=Falseif x<0:
sign =True
x=-x
li=[]while x>0:
li.append(x%10)
x/=10
base=1
result=0for i inrange(len(li)-1,-1,-1):
result+=base*li[i]
base*=10if result>2**31-1:return0if sign:return-result
else:return result
优秀代码
classSolution(object):defreverse(self, x):"""
:type x: int
:rtype: int
"""
int_max =2**31
reverse =0
flag =0if x<0:
x =abs(x)
flag =1while x !=0:
digit = x%10
reverse = reverse*10+ digit
x = x //10if(reverse > int_max):return0if flag ==1:
reverse = reverse*(-1)return reverse