7. 整数反转(python3)

Python:

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x == 0:
            return 0
        
        str_x = str(x)
        temp1 = ""
        temp2 = ""
        if str_x[0] == "-":
            temp1 = "-"
        else:
            pass
        temp2 = temp1 + str_x[::-1].lstrip("0").rstrip("-")
        temp2 = int(temp2)
        if -2**31 < temp2 < 2**31 - 1:
            return temp2
        return 0

猜你喜欢

转载自blog.csdn.net/weixin_42158523/article/details/85246970