LC 0007 [M] Reverse Integer - ALawliet/algorithms GitHub Wiki
class Solution:
def reverse(self, x: int) -> int:
sign = 1
if x < 0:
sign = -1
res = 0
min_int = 2**31
max_int = 2**31 - 1
x = abs(x)
while x > 0:
q, r = divmod(x, 10)
if sign == 1 and res > (max_int - r) / 10:
return 0
if sign == -1 and res > (min_int - r) / 10:
return 0
res = res * 10 + r
x = q
return sign * res
class Solution:
def reverse(self, x):
sign = [1,-1][x<0]
rev, x = 0, abs(x)
while x:
x, mod = divmod(x, 10)
rev = rev*10 + mod
return sign*rev if -pow(2,31) <= sign*rev <= pow(2,31)-1 else 0
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
sign = [1,-1][x < 0]
rst = sign * int(str(abs(x))[::-1])
return rst if -(2**31)-1 < rst < 2**31 else 0