moonlightshadow123
5/30/2017 - 3:14 PM

7. Reverse Integer

  1. Reverse Integer
class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        isPositive = x >= 0
        x = abs(x)
        res = 0
        while x != 0:
            res = res * 10 + x % 10
            x /= 10
        res = res if isPositive else -res    
        
        if res > 0x7fffffff or res < -(0x80000000):
            return 0
        return res

https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

Example1: x = 123, return 321 Example2: x = -123, return -321