题目描述
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
思路
先转字符串,符号位单独考虑。对只有数字的字符串进行反转,再转为整数型,最后与符号位相乘输出
import math
class Solution:
def reverse(self, x: int) -> int:
label=1
begin=0
s=str(x)
if s[0]=="+":
label=1
begin=1
if s[0]=='-':
label=-1
begin=1
s=s[begin:len(s)]
s=s[::-1]
res=int(s)
return res*label if -math.pow(2,31) < res*label < math.pow(2,31)-1 else 0