dsa · medium
Divide Two Integers
Given integers dividend and divisor (divisor is never 0), return the quotient dividend / divisor truncated toward zero. Do not use the multiplication, division, or remainder operators.
Arguments
dividend— the integer being divideddivisor— the non-zero integer to divide by
The result must fit a 32-bit signed integer. If the true quotient is strictly greater than 2^31 - 1, return 2^31 - 1. If it is strictly less than -2^31, return -2^31.
Example
dividend = 10, divisor = 3: 10/3 is 3.333…. Truncating toward zero drops the fraction, so the answer is 3.
dividend = 7, divisor = -3: 7/(-3) is -2.333…. Toward zero is -2 (not floor, which would be -3).
dividend = 0, divisor = 1 → 0.
Constraints
-2^31 <= dividend, divisor <= 2^31 - 1 divisor != 0
Examples
Example 1
Input: 10 3 Expected: 3
Example 2
Input: 7 -3 Expected: -2
Example 3
Input: 0 1 Expected: 0