Python solution. No idiotic str() which is missing the point of the problem.


  • 0
    P

    For some reason most Python solutions convert to string and back, which in my mind is totally missing the point of the problem.
    So here is my Python solution that uses math instead of string conversions.

    class Solution(object):                                                                                           
        def reverse(self, x):                                                                                                 from math import log                                                                                                                                                                                                        
            if x < 10 and x > -10:                                                                                    
                return x                                                                                                                                                                                                                
            flipped = False                                                                                                                                                                                                             
            if x < 0:                                                                                                 
                flipped = True                                                                                        
                x *= -1                                                                                                                                                                                                                 
            res = 0                                                                                                   
            log10 = int(log(x, 10))                                                                                                                                                                                                     
            for i in xrange(log10 + 1):                                                                               
                digit = x % 10                                                                                       
                res += digit * 10**(log10 - i)                                                                        
                x /= 10                                                                                                                                                                                                                                                                                                                               
            if res > 2**31 - 1 or res < -1 * 2**31 + 1:                                                               
                return 0                                                                                                                                                                                                                
            if flipped:                                                                                               
                res *= -1                                                                                                                                                                                                              
            return res  
    

Log in to reply
 

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