7.整数反转(通过)
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
y = abs(x)
l = list(str(y))
llong = len(l)
result = 0
for i in range(llong-1,-1,-1):
l[i] = int(l[i])
result = result + l[i] * (10**i)
if result >=2**31-1 or result <= -2**31:
return 0
elif x <= 0:
result = "-%s" % result
result = int(result)
return result
else:
return result