My accepted python solution


  • 0
    Y

    I use the positive to save the +, and the reverse the list, and finally add the positive back to the reversed list

        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            max=2**31-1
            reverse=[]
            reverse_int=0
            positive=True
            count=0
            if x<0:
                x=-x
                positive=False
           for i in str(x):
                reverse.append(i)
            count=len(reverse)
            for j in reverse[::-1]:
                reverse_int=reverse_int+int(j)*10**(count-1)
                count=count-1
            if reverse_int>max:
                return 0
            elif positive:
                return reverse_int
            else:return (-reverse_int)
    

Log in to reply
 

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