题目描述
description.png思路:这题用python首先想到的就是转化成数组,用[::-1]来做。但是为了好好学习,还是少用点内置算法。x右移/10,num左移*10,用while
class Solution:
def reverse(self, x: int) -> int:
y = abs(x)
nums = 0
while(y!=0):
num = y%10
nums = nums*10 + num
y = y//10
if nums>- pow(2,31) and nums < pow(2,31)-1:
return nums if x > 0 else -nums
else:
return 0
另外用python反转的方法:
class Solution:
def reverse(self, x: int) -> int:
y,num = abs(x),0
num = int(str(y)[::-1])
if num > -pow(2,31) and num < pow(2,31)-1:
return num if x>0 else -num
else:
return 0
网友评论