Leetcode整数反转
直接上python代码
class Solution:
def reverse(self, x: int) -> int:
sws = []
for i in str(abs(x)): //先不管正负,直接取绝对值
sws.insert(0,i) //每次都在开头插入字符
sws = ''.join(sws) //把字符转换成整数
if int(sws)>2**31-1: //根据题目所给条件来限制输出
return 0
elif x<0:
return -int(sws)
else:
return int(sws)