29. Divide Two Integers
class Solution {
public:
const int MAX_INT = 2147483647;
typedef long long ll;
int divide(int dividend, int divisor) {
ll a = abs(ll(dividend)); //pay attention to negative num &&&& not abs(dividend);
ll b = abs(ll(divisor));
cout<<a<<' '<<b<<endl;
ll res = 0;
while (a >= b) {
ll tmp = b;
ll cnt = 1;
while ((tmp << 1) <= a) {
tmp <<= 1;
cnt <<= 1;
}
res += cnt;
a -= tmp;
}
if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)) res = -res;
if (res > MAX_INT) res = MAX_INT;
return res;
}
};