Solution in Python 2


  • 0
    A
    def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            digit = []
            count = 0
            if x == 0:
                return x
            elif x > 0:
                while x/10 != 0:
                    digit.append(x%10)
                    x = x/10
                    count = count + 1
                digit.append(x)
                i = 0
                res = 0
                while not count < 0:
                    res += digit[count] * pow(10,i)
                    i = i+1
                    count = count-1
            else:
                l = []
                l[:] = list(str(x))
                l.pop(0)
                l.append('-')
                l.reverse()
                new_x = ''.join(l[:])
                res = int(new_x)
                
            if res >= -2147483648 and res <= 2147483647:
                return res
            else:
                return 0
                
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.