Divide Two Integers
Solution ApproachWas this helpful?
Medium•bits•Expected: O(log^2 n) where n = dividend time, O(1) space
bit-manipulationmath
Problem
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.
Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [-2^31, 2^31 - 1]. If the quotient is strictly greater than 2^31 - 1, return 2^31 - 1, and if the quotient is strictly less than -2^31, return -2^31.
Constraints: -2^31 <= dividend, divisor <= 2^31 - 1. divisor != 0.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333.. which is truncated to 3.
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333.. which is truncated to -2.
Reference solution unlocks after your first submission
Loading...
