Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yitonghe00/5f4d3e7a57bb1f24009b78d2569e4e41 to your computer and use it in GitHub Desktop.
Save yitonghe00/5f4d3e7a57bb1f24009b78d2569e4e41 to your computer and use it in GitHub Desktop.
By Siyu Lei
class Solution {
public int divide(int dividend, int divisor) {
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean sign = (dividend > 0) ^ (divisor > 0);
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
int res = 0;
while (dvd >= dvs) {
long temp = dvs;
int multiple = 1;
while (dvd >= (temp << 1)) {
multiple <<= 1;
temp <<= 1;
}
dvd -= temp;
res += multiple;
}
return sign ? -res : res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment