Description
Submission
class Solution {
public:
int divide(int dividend, int divisor) {
long a = dividend;
long b = divisor;
bool isNeg = false;
if(a < 0) {
a = -a;
isNeg = !isNeg;
}
if(b < 0) {
b = -b;
isNeg = !isNeg;
}
if(b > a) return 0;
long count = 0;
while(b <= a) {
int c = 0;
for(; (b << (c + 1)) < a; ++c);
a -= (b << c);
count += (1 << c);
}
if(count > 2147483647 && !isNeg) return 2147483647;
return isNeg ? -count : count;
}
};
References
- https://www.programcreek.com/2014/05/leetcode-divide-two-integers-java/