Created
May 23, 2014 03:07
-
-
Save Cee/60f2bf9d7cd63ce5baca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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