Divide Two Integers
Question
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
Solution
Break down: Example 10/3: 10 = 32 + 31 + ? Example 123/7: 123 = 716 + 71 + ? Suppose both divident and divisor are positive number:
Way to prevent overflow issue:
Since negative number has wider range than positive number, we use negative for the computation:
Last updated