1. 程式人生 > >7. 整數反轉(python3)

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