Python TLE, help


  • 0
    X
    class Solution(object):
        def divide(self, dividend, divisor):
           
            if divisor == 0:
                return -1
            
            mark = ''
            
            if dividend >= 0 and divisor < 0 or dividend <= 0 and divisor > 0:
                mark = '-'
            
            i = 0
            a = abs(dividend)
            b = abs(divisor)
            while a - b >= 0:
                a = a - b
                i += 1
            
            return int(mark + str(i))
    

    I browsed some answers and found they did it using similar method. But why do I get TLE all the time? Any better solutions?


  • 0
    X

    Ahhh, I found the difference.

    I didn't really understand their algorithm.

    I've corrected my method. :dancer:


Log in to reply
 

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