Given a 32-bit signed integer, reverse digits of an integer.
Input: 123 Output: 321
Input: -123 Output: -321
Input: 120 Output: 21
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
class Solution:
def reverse(self, x: int) -> int:
result = 0
xc = abs(x)
while xc != 0:
result *= 10
result += xc % 10
xc //= 10
if result < -(2 ** 31) or result > (2 ** 31) - 1:
return 0
elif x >= 0:
return result
else:
return -result
int reverse(int x)
{
int Int_Max = 0x7fffffff;
int Int_Min = 0x80000000;
long sum = 0;
for( ; x; x /= 10 )
sum = ( sum *= 10 ) + x % 10;
if( sum > Int_Max || sum < Int_Min )
return 0;
return sum;
}