[LeetCodeOJ] Reverse Integer

来源:互联网 发布:方形补偿器计算软件 编辑:程序博客网 时间:2024/06/05 20:52

Reverse digits of an integer.

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

click to show spoilers.

Subscribe to see which companies asked this question

class Solution(object):    def reverse(self, x):        num = 0        if x > 0:            sig = 1        else:            sig = -1        x = abs(x)        while(x != 0):            temp = x % 10            num = num * 10 + temp            x = x / 10        if num > math.pow(2, 31):            return 0        else:            return num*sig
0 0