大部分报绿的RD都集中在2014年4-6月左右# EB23 - 劳工卡
c*r
1 楼
我看了下这题的code,看了半天没搞懂这里边的位移运算是在干嘛。 为啥不能直接
while loop里每次a-b然后count次数?
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function
int a = Math.abs(dividend);
int b = Math.abs(divisor);
//b maybe Integer.MIN_VALUE
boolean neg = (dividend > 0 && divisor < 0) || (dividend < 0 &&
divisor > 0);
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (divisor == Integer.MIN_VALUE) {
if (dividend == Integer.MIN_VALUE) {
return 1;
}
else {
return 0;
}
}
if (dividend == Integer.MIN_VALUE) {
if (neg) {
return -1 + divide(dividend + b, b);
}
else {
return 1 - divide(dividend + b, b);
}
}
int product = b, result = 0;
while (a >= b) {
int q = 1;
while (a - product >= product) {
q = q << 1;
product = product << 1;
}
a = a - product;
product = b;
result += q;
}
if (!neg) {
return result;
}
else {
return -result;
}
}
}
while loop里每次a-b然后count次数?
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function
int a = Math.abs(dividend);
int b = Math.abs(divisor);
//b maybe Integer.MIN_VALUE
boolean neg = (dividend > 0 && divisor < 0) || (dividend < 0 &&
divisor > 0);
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (divisor == Integer.MIN_VALUE) {
if (dividend == Integer.MIN_VALUE) {
return 1;
}
else {
return 0;
}
}
if (dividend == Integer.MIN_VALUE) {
if (neg) {
return -1 + divide(dividend + b, b);
}
else {
return 1 - divide(dividend + b, b);
}
}
int product = b, result = 0;
while (a >= b) {
int q = 1;
while (a - product >= product) {
q = q << 1;
product = product << 1;
}
a = a - product;
product = b;
result += q;
}
if (!neg) {
return result;
}
else {
return -result;
}
}
}