Divide Two Integers

Desicription

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int divide(int dividend, int divisor) {
if(!divisor || (dividend == INT_MIN && divisor == -1))
return INT_MAX;
int flag = ((dividend < 0) ^ (divisor < 0))?-1:1;
long long m = abs((long long)dividend);
long long n = abs((long long)divisor);
int res = 0;
while(m >= n){
long long t = n;
long long p = 1;
while((t << 1) <= m){
t <<= 1;
p <<= 1;
}
m -= t;
res += p;
}
return flag*res;
}
};