Number in Python doesn't have limit, for the overflow case.

• Number in Python doesn't have limit, either the input or output number won't overflow anyway.
But OJ still requires 0 as outcome of the huge reversed number.

• So how do we avoid this?

• Hard code the limit and it works (I dont like hard code, honestly)
Anyone has better idea?
here is my code:

import sys

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""

``````    # sys.maxint equals to 2147483647
# I dont like hard code...
strx = str(int(x))
if x >= sys.maxint or x<-sys.maxint:
return 0
elif strx[0] == '-':
if int('-'+(strx[1:])[::-1]) > -2147483647:
return int('-'+(strx[1:])[::-1])
else:
return 0

else:
if int(strx[::-1]) < 2147483647:
return int(strx[::-1])
else:
return 0``````

• Use `0x7FFFFFFF` may be better:

``````class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
digits = []

if x==0:
return 0

remains = abs(x)
sign = -1 if x < 0 else 1

while(True):
# remains is not zero
digit = remains % 10
remains = remains / 10
digits.append(digit)
if remains == 0:
break

ret = 0
for i in digits:
ret *= 10
ret += i

ret *= sign
if abs(ret) > 0x7FFFFFFF:
return 0
else:
return ret
``````

• combine two loop may be easier to read:

class Solution(object):

``````def reverse(self, x):
ret = 0

if x==0:
return 0

remains = abs(x)
sign = -1 if x < 0 else 1

while(True):
# remains is not zero
digit = remains % 10
remains = remains / 10
ret = ret * 10 + digit
if remains == 0:
break

ret *= sign
if abs(ret) > 0x7FFFFFFF:
return 0
else:
return ret``````

• In Python 3.5+ division operator does floating point division. We need to use // to do integer division.

``````class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
m = -1 if x < 0 else 1
x = x*m

n = 0
while x > 0:
n = (n * 10) + (x % 10)
x = x // 10

if n > 0x7FFFFFFF:
return 0

return n*m
``````

• Avoid this by not hiring whoever wrote the tests for this problem.

• This post is deleted!

• The range of 32-bit values is [-231, 231-1]. So comparing to 0x7FFFFFFF is wrong because minimum 32-bit value is -231 or 0x80000000.

Wrong result would come from reverse(-8463847412) which would return 0 instead of -2147483648 (-231).

• This post is deleted!

• @hanfeisun for remains, will it need to add a int(remain)?

• use checks such as 'isinstance()' or 'type(var)' to determine if given number is in the integer range.

x=999999999999999999
type(x) is int
True

isinstance(x,int)
True

x=999999999999999999999999999999999999999999999999999
isinstance(x,int)
False

type(x) is int
False

type(x)
<type 'long'>

• #This is my solution
import numpy
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
slogan=0
if x<0:
slogan=1
x=abs(x)
L=[]
while(x):
d=x%10
L.append(d)
x=x/10
sum=0
for i in range(len(L)):
sum+=L[i]*pow(10,len(L)-i-1)
if sum>2147483647:
return 0
if(slogan):
return int('-'+str(sum))
else:
return sum

• class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
digits = []

``````    if x==0:
return 0

remains = abs(x)
sign = -1 if x < 0 else 1

while(True):
# remains is not zero
digit = remains % 10
remains = remains / 10
digits.append(digit)
if remains == 0:
break

ret = 0
for i in digits:
ret *= 10
ret += i

ret *= sign
if abs(ret) > 0x7FFFFFFF:
return 0
else:
return ret
``````

7463847412 doesn't work on test case although it assume the input is 32 bits, the range should be 2147483647 to -2147483648, one number is missing if you use absolute = 0x7FFFFFFF

• ``````-1<<31 < n < (1<<31)-1
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.