Python's version of solution


  • 0
    class Solution:
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            
            self.x = x
            
            #to handle the negative numbers:
            if x < 0:
                
                x = str(x)
                new_x = ""
                for i in range(len(x)-1,0,-1):
                    new_x = new_x + x[i]
                
                new_x = int(new_x) * (-1)
                
            
            else:
                
                x = str(x)
                new_x = ""
                for i in range(len(x)-1,-1,-1):
                    new_x = new_x + x[i]
                
                new_x = int(new_x)
                
            #make sure the answer is within 32-bit range; otherwise, return zero:
            if -0x80000000 < new_x < 0x7fffffff:
                return new_x
            else:
                return 0
    

Log in to reply
 

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