7、整数反转

7、整数反转

难度:简单
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

Python:

初始版本:44ms

class Solution:
    def reverse(self, x: int) -> int:
        if_neg = 1
        if_overflow = 1
        output = 0

        if x < 0:
            if_neg = -1
            x = -x
        
        while x != 0:
            output = output * 10 + x % 10
            x = int(x/10)
        
        output = if_neg * output

        if (output < -2**31) or (output > 2**31 -1):
            if_overflow = 0

        return if_overflow * output
posted @ 2020-08-20 22:57  ZeroCrow  阅读(90)  评论(0)    收藏  举报