Leetcode 29 - Divide two ints

Note:

• Take 100 / 3 for example:
• If we just do 100 - 3 every time and count the num, it’s be really slow!
• Instead, we use >>> unsigned left shift to divide dividend every time, so we can quicklly find the first number that is bigger than divisor.
• BTW, we use >>> instead of divisor << i coz it can prevent overflow.
• Make sure 100 >>> i >= 3.
• After we find 100 >>> 1 for 5 times, which means we’ve found 2^5 * 3 <= 100;
• Then 100 - 32*3 = 4
• Let dividend = 4, and add 32 to res. Do the iteration again.
• Use >>> on 2**31 otherwise you’ll get a negative number coz 2**31‘s first digit is 1 and JS will assume its a negative number after shifting it.

Question:

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.

Return the quotient after dividing dividend by divisor.

Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231.

Example:

Code: