How can i modify this code in order to make it faster?


  • 0
    N
    /**
     * @param {number} dividend
     * @param {number} divisor
     * @return {number}
     */
    var divide = function(dividend, divisor) {
        var ostatok = 0;
        var result = 0;
        var minus = false;
        
        if(dividend < 0 && divisor < 0){
            // Make dividend and divisor +
            dividend = dividend - dividend - dividend;
            divisor = divisor - divisor - divisor;
        } else if(dividend < 0){
            // Make dividend +
            dividend = dividend - dividend - dividend;
            minus = true;
        } else if(divisor < 0){
            // Make divisor +
            divisor = divisor - divisor - divisor;
            minus = true;
        }
        ostatok = dividend;
        while(ostatok > 0){
            ostatok = ostatok - divisor;
            if(ostatok < 0){
                break;
            } else {
                result = result + 1;
            }
            
        }
        if(minus){
             // If fividend or divisor where -, make answer with - sign
             result = result - result - result;
             return result;
        } else {
             return result;
        }
       // Please sorry for my English.
    };
    

Log in to reply
 

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