Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
class Solution {
public:
int divide(int dividend, int divisor) {
long a = labs(dividend);
long b = labs(divisor);
long ans = 0;
while(a>=b){
long d = b;
long t = 1;
while((d<<1) < a){d<<=1;t<<=1;}
ans += t;
a -= d;
}
if((dividend<0 && divisor>0)||(dividend>0 && divisor<0)){
if(-1*ans<INT_MIN) return INT_MIN;
return -1*ans;
}
return ans>INT_MAX?INT_MAX:ans;
}
};