标签:object ret turn def class 数字 == leetcode tco
思路
python提供了方便的字符串反转方法,所以还是蛮简单的这题
注意几个坑:
代码
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
s = str(x)
if s[0] == '-':
num = s[1:].lstrip('0')
x = -int(num[::-1])
if x > pow(2,31)-1 or x < -pow(2,31):
return 0
else:
return x
elif len(s) == 1:
return int(s)
else:
x = int(s[::-1].lstrip('0'))
if x > pow(2,31)-1 or x < -pow(2,31):
return 0
else:
return x
#每种情况都判断一次是否溢出稍显繁琐,可以把它放在最后的return 语句里顺便判断
标签:object ret turn def class 数字 == leetcode tco
原文地址:https://www.cnblogs.com/MartinLwx/p/9688750.html