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
Note:
- Both dividend and divisor will be 32-bit signed integers.
- The divisor will never be 0.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 2^31 − 1 when the division result overflows.
方法一: 位运算法
In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can we use? Yeah, bit manipulations.
Let's do an example and see how bit manipulations work.
Suppose we want to divide 15
by 3
, so 15
is dividend
and 3
is divisor
. Well, division simply requires us to find how many times we can subtract the divisor
from the the dividend
without making the dividend
negative.
Let's get started. We subtract 3
from 15
and we get 12
, which is positive. Let's try to subtract more. Well, we shift 3
to the left by 1
bit and we get 6
. Subtracting 6
from 15
still gives a positive result. Well, we shift again and get 12
. We subtract 12
from 15
and it is still positive. We shift again, obtaining 24
and we know we can at most subtract 12
. Well, since 12
is obtained by shifting 3
to left twice, we know it is 4
times of 3
. How do we obtain this 4
? Well, we start from 1
and shift it to left twice at the same time. We add 4
to an answer (initialized to be 0
). In fact, the above process is like 15 = 3 * 4 + 3
. We now get part of the quotient (4
), with a remainder 3
.
Then we repeat the above process again. We subtract divisor = 3
from the remaining dividend = 3
and obtain 0
. We know we are done. No shift happens, so we simply add 1 << 0
to the answer.
Now we have the full algorithm to perform division.
According to the problem statement, we need to handle some exceptions, such as overflow.
Well, two cases may cause overflow:
divisor = 0
;dividend = INT_MIN
anddivisor = -1
(becauseabs(INT_MIN) = INT_MAX + 1
).
Of course, we also need to take the sign into considerations, which is relatively easy.
Putting all these together, we have the following code.
class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor || (dividend == INT_MIN && divisor == -1))
return INT_MAX;
int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
long long dvd = labs(dividend);
long long dvs = labs(divisor);
int res = 0;
while (dvd >= dvs) {
long long temp = dvs, multiple = 1;
while (dvd >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dvd -= temp;
res += multiple;
}
return sign == 1 ? res : -res;
}
};
Runtime: 24 ms
The outer loop reduces n by at least half each iteration. So It has O(log N) iterations.
The inner loop has at most log N iterations.
So the overall complexity is O(( log N)^2)
方法二:对数运算法
class Solution {
public:
int divide(int dividend, int divisor) {
/** a/b = e^(ln(a))/e^(ln(b)) = e^(ln(a)-ln(b)) **/
if(dividend==0) return 0;
if(divisor==0) return INT_MAX;
double t1=log(fabs(dividend));
double t2=log(fabs(divisor));
long long result=double(exp(t1-t2));
if((dividend<0) ^ (divisor<0)) result=-result;
if(result>INT_MAX) result=INT_MAX;
return result;
}
};
方法三: 32 times bit shift operation with O(1) solution
we assure the factor ret
's binary fomula is
ret = a0 + a1*2 + a2*2^2 + ...... + a29*2^29 + a30*2^30 + a31*2^31; ai = 0 or 1, i = 0......31
the dividend B
and divisor A
is non-negative, then
A(a0 + a1*2 + a2*2^2 + ...... + a29*2^29 + a30*2^30 + a31*2^31) = B; Eq1
(1) when Eq1
divided by 2^31
, we can get A*a31 = B>>31
; then a31 = (B>>31)/A
;
if (B>>31) > A
, then a31 = 1
; else a31 = 0
;
(2) when Eq1
divided by 2^30
, we can get A*a30 + A*a31*2 = B>>30
; then a30 = ((B>>30) - a31*A*2)/A
; and (B>>30) - a31*A*2
can be rewritten by (B-a31*A<<31)>>30
, so we make B' = B-a31*A<<31
, the formula simplified to a30 = (B'>>30)/A
if (B'>>30) > A
, then a30 = 1
; else a30 = 0
;
(3) in the same reason, we can get a29 = ((B-a31*A<<31-a30*A<<30)>>29)/A
, we make B'' = B' - a30*A<<30
, the formula simplified to a29 = (B''>>29)/A
;
do the same bit operation 32
times, we can get a31 ..... a0
, so we get the ret
finally.
class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor)
return INT_MAX;
if (divisor == 1)
return dividend;
if (divisor == -1) {
if (dividend == INT_MIN)
return INT_MAX;
else
return -dividend;
}
bool sign = (dividend > 0) ^ (divisor > 0);
unsigned int B = abs(dividend);
unsigned int A = abs(divisor);
int ret = 0;
for (int i = 31; i >= 0; --i) {
if ((B >> i) >= A) { //ai = 1
ret = (ret << 1) | 0x01;
B -= (A << i); //update B
}
else //ai = 0
ret = ret << 1;
}
if (sign)
ret = -ret;
return ret;
}
};
Runtime:16ms