class Solution:
def reverse(self, x: int) -> int:
str_x = str(x)
negative = False
if str_x[0] == "-":
negative = True
str_x = str_x[1:]
zero_count = 0
for i in str_x[::-1]:
if i != 0:
break
zero_count += 1
if not negative:
rlt = str_x[::-1][zero_count:]
else:
rlt = "-" + str_x[::-1][zero_count:]
int_rlt = int(rlt)
if int_rlt < -2**31 or int_rlt > 2**31-1:
int_rlt = 0
return int_rlt
7,整数反转
最新推荐文章于 2022-10-16 20:59:24 发布