class Solution {
public int divide(int dividend, int divisor) {
boolean isNeg = (dividend < 0) != (divisor < 0);
long absDividend = Math.abs(new Long(dividend));
long absDivisor = Math.abs(new Long(divisor));
long sum = 0;
while (absDivisor <= absDividend) {
int counter = 0;
long temp = absDivisor;
while ((temp << 1) < absDividend) {
temp = temp << 1;
counter++;
}
sum += 1 << counter;
absDividend -= temp;
}
if (isNeg) {
if (-sum < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) -sum ;
}
if (sum > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int)sum;
}
}
No comments:
Post a Comment