Divide Two Integers
Medium
Topics
Given two integers dividend and divisor, divide them without using multiplication, division, or the modulo operator.
The integer division should truncate toward zero, so 8.345 becomes 8 and -2.7335 becomes -2.
Assume 32-bit signed integers. If the true quotient exceeds the representable range, return 2^31 - 1.
Example 1
Input: a = 10, b = 3 Output: 3 Explanation: 10 / 3 = 3.333 truncates to 3.
Example 2
Input: a = 7, b = -3 Output: -2 Explanation: 7 / -3 = -2.333 truncates toward zero, giving -2.
Example 3
Input: a = -2147483648, b = -1 Output: 2147483647 Explanation: The true answer 2147483648 overflows, so it is clamped.
Constraints
- -2^31 <= dividend, divisor <= 2^31 - 1
- divisor != 0
Run ⌘' · Submit ⌘⏎