LeetCode 29: Divide Two Integers
Problem Description
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: [−2^31, 2^31 − 1]
. For this problem, if the quotient is strictly greater than 2^31 - 1
, then return 2^31 - 1
, and if the quotient is strictly less than -2^31
, then return -2^31
.
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.
Solution
The first idea is to count how many times we can subtract divisor
from dividend
. At the same time, we have to take care of overflows and corner cases. Since the range of negative numbers is larger than that of positive numbers, we'd better work in negative domain.
Repeating subtracting could be slow, to improve the performance, we can speed up by exponential search or binary search in some sense. That is, we keep doubling the divisor
until it can be subtracted from dividend
, and start subtracting this highest doubler and then the smaller doublers. Either we can store all doublers into an array or use the bit operation to get the next smaller doubler.
Java Code
Brute Force
class Solution {
public int divide(int dividend, int divisor) {
if(divisor == 1) return dividend;
if(divisor == -1) return dividend==Integer.MIN_VALUE ? Integer.MAX_VALUE : -dividend;
int res=0, sign=1;
if(dividend<0) sign = -sign;
else dividend = -dividend;
if(divisor<0) sign = -sign;
else divisor = -divisor;
while(dividend <= divisor){
res--;
dividend -= divisor;
}
return sign<0 ? res : -res;
}
}
Exponential Search, Array stored
class Solution {
public int divide(int dividend, int divisor) {
if(divisor == 1) return dividend;
if(divisor == -1) return dividend==Integer.MIN_VALUE ? Integer.MAX_VALUE : -dividend;
int res=0, sign=1, half_int = (Integer.MIN_VALUE+1)>>1;
if(dividend<0) sign = -sign;
else dividend = -dividend;
if(divisor<0) sign = -sign;
else divisor = -divisor;
List<Integer> highest_divisors = new ArrayList<>(), copies = new ArrayList<>();
int copy = 1;
while( dividend <= divisor ){
highest_divisors.add( divisor );
copies.add( copy );
if( divisor < half_int ) break;
divisor += divisor;
copy += copy;
}
for(int i=highest_divisors.size()-1; i>=0; i--){
if( dividend<=highest_divisors.get(i) ){
res -= copies.get(i);
dividend -= highest_divisors.get(i);
}
}
return sign<0 ? res : -res;
}
}
Exponential Search, bit operation
class Solution {
public int divide(int dividend, int divisor) {
if(divisor == 1) return dividend;
if(divisor == -1) return dividend==Integer.MIN_VALUE ? Integer.MAX_VALUE : -dividend;
int res=0, sign=1, half_int = (Integer.MIN_VALUE+1)>>1;
if(dividend<0) sign = -sign;
else dividend = -dividend;
if(divisor<0) sign = -sign;
else divisor = -divisor;
int highest_divisor=divisor, copy = 1;
while( highest_divisor>=half_int && dividend <= (highest_divisor+highest_divisor) ){
highest_divisor += highest_divisor;
copy += copy;
}
while( dividend <= divisor ){
if( dividend<=highest_divisor ){
res -= copy;
dividend -= highest_divisor;
}
copy >>= 1;
highest_divisor >>= 1;
}
return sign<0 ? res : -res;
}
}
The post is published under CC 4.0 License.