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.
Number in Python doesn't have limit, for the overflow case.

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


use checks such as 'isinstance()' or 'type(var)' to determine if given number is in the integer range.
x=999999999999999999
type(x) is int
Trueisinstance(x,int)
Truex=999999999999999999999999999999999999999999999999999
isinstance(x,int)
Falsetype(x) is int
Falsetype(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)i1)
if sum>2147483647:
return 0
if(slogan):
return int(''+str(sum))
else:
return sum

@hanfeisun said in Number in Python doesn't have limit, for the overflow case.:
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