Use string manipulation instead of arithmetic operations.
def reverse(self, x): """ :type x: int :rtype: int """ if x < 0: y = -1 * int( str(-x)[::-1] ) else: y = int( str(x)[::-1] ) if y > 2147483647 or y < -2147483648: y = 0 return y
I don't like solutions like this.
It's not like you thinking of converting it to string is anywhere near original. Everyone reading this problem thinks of string conversions first. Problem is, with that method you learn nothing. Every python or non python coder who has been coding for a few days can do this with your proposed method. It's no challenge whatsoever.
I wish people would stop posting these kinds of solutions.