SHARE
TWEET

Untitled

a guest Oct 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @param {number} dividend
  3.  * @param {number} divisor
  4.  * @return {number}
  5.  */
  6. var divide = function(dividend, divisor) {
  7.     if (dividend == INT_MIN && divisor == -1) {
  8.         return INT_MAX;
  9.     }
  10.  
  11.     const sign = (dividend > 0 && divisor > 0 || dividend < 0 && divisor < 0) ? 1 : -1;
  12.     dividend = Math.abs(dividend);
  13.     divisor = Math.abs(divisor);
  14.  
  15.     let ans = 0;
  16.  
  17.     while (dividend >= divisor) {
  18.         let shifts = 1;
  19.         let curr = divisor;
  20.  
  21.         while (curr <= (dividend >> 1)) {
  22.             curr <<= 1;
  23.             shifts <<= 1;
  24.         }
  25.         ans += shifts;
  26.         dividend -= curr;
  27.     }
  28.     return sign * ans;
  29. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top