两数相除--位运算
作者:互联网
英语版
中文版
这道题思路跟一个up主一样 就是细节处理不是很好 ~~~~ x1 x2 x4 x8 而我没有导致超时
public int divide(int dividend, int divisor) {
if (divisor == 0)
return Integer.MAX_VALUE;
if (dividend == Integer.MIN_VALUE) {
if (divisor == -1)
return Integer.MAX_VALUE;
else if (divisor == 1)
return Integer.MIN_VALUE;
}
long divi = dividend;
long divs = divisor;
int sign = 1;
if (divi < 0) {
sign = -sign;
divi = -divi;
}
if (divs < 0) {
sign = -sign;
divs = -divs;
}
int res = 0;
while (divi >= divs) {
int shift = 0 ;
while (divi >= divs<<shift) {
shift++;
}
res += (1<<(shift-1));
divi -= (divs<<(shift-1));
}
return res * sign;
}
其他解题思路
https:// leetcode-cn.com/problems/divide-two-integers/
标签:运算,divi,int,相除,sign,Integer,divs,divisor 来源: https://blog.51cto.com/14429166/2417903