Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 23, 2014 03:07
Show Gist options
  • Save Cee/60f2bf9d7cd63ce5baca to your computer and use it in GitHub Desktop.
Save Cee/60f2bf9d7cd63ce5baca to your computer and use it in GitHub Desktop.
public class Solution {
public int divide(int dividend, int divisor) {
long a = dividend;
long b = divisor;
if (a == 0) return 0;
boolean flag = (a > 0) ^ (b > 0);
if (a < 0) a = -a;
if (b < 0) b = -b;
if (a < b) return 0;
long ret = 0;
long count = 1;
long k = b;
while (k < a){
k = k << 1;
count = count << 1;
}
while (count >= 1){
if (a >= k){
a -= k;
ret += count;
if (a == 0) break;
}
k = k >> 1;
count = count >> 1;
}
if (!flag) {
return (int)(ret);
}else{
return -(int)(ret);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment