LeetCode/Python: 7. Reverse Integer

class Solution():
    def reverse(self, x):
        # Note that in Python -1 / 10 = -1
        res, isPos = 0, 1
        if x < 0:
            isPos = -1
            x = -1 * x
        while x != 0:
            res = res * 10 + x % 10
            if res > 2147483647:
                return 0
            x = int(x / 10)
        return res * isPos
s = Solution()
print (s.reverse(123))

猜你喜欢

转载自www.cnblogs.com/lijinghuabj/p/9211990.html