您的位置:首页 > 其它

LeetCode: Divide Two Integers 解题报告

2014-10-24 20:15 344 查看
[b]Divide Two Integers[/b]

public int divide(int dividend, int divisor) {
long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor);

long ret = 0;

while (a >= b) {
for (long tmp = b, cnt = 1; a >= tmp; tmp <<= 1, cnt <<= 1) {
ret += cnt;
a -= tmp;
}
}

ret = (((dividend ^ divisor) >> 31) & 1) == 1 ? -ret: ret;
if (ret > Integer.MAX_VALUE || ret < Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
}

return (int)ret;
}


View Code

GitHub Code:


divide.java

Ref: /article/1340720.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: