Given two integers dividend
and divisor
, divide
two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend
by
divisor
.
The integer division should truncate toward zero.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Note:
Every decimal number can be represented as
a0*2^0 + a1*2^1 + a2*2^2 + ... + an*2^n
.
Replace multiplication and division with binary shifting.
/**
* @param {number} dividend
* @param {number} divisor
* @return {number}
*/
let divide = function(dividend, divisor) {
if (divisor === 0 ||
divisor === -1 && dividend < -2147483647 ||
dividend > 2147483647 ||
dividend < -2147483648
) {
return 2147483647
}
const isNegative = dividend < 0 && divisor >= 0 || dividend >= 0 && divisor < 0
const pDividend = Math.abs(dividend)
const pDivisor = Math.abs(divisor)
if (dividend === 0 || pDividend < pDivisor) { return 0 }
let doubling = pDivisor
let count = 1
while (doubling < pDividend && !(doubling & (1 << 30))) {
doubling <<= 1
count <<= 1
}
if (doubling > pDividend) {
doubling >>>= 1
count >>>= 1
}
const result = count + divide(pDividend - doubling, pDivisor)
return isNegative ? -result : result
};