public int guessNumber(int n) {
int min = 1, max = n;
while (min <= max) {
// int mid = (min + max) / 2; 会超时
int mid = min+(max-min ) / 2;//不超时
int guess = guess(mid);
if (guess == 1) {
min = mid + 1;
} else if (guess == -1) {
max = mid - 1;
} else {
return mid;
}
}
return min;
}
请问为什么一个超时一个不超时
1
chy373180 2017-12-01 10:27:55 +08:00
int mid = (min + max) / 2 int 会 overflow
|
3
zjbztianya 2017-12-01 10:41:08 +08:00
@MrXiong 溢出了循环还在跑啊。只是是个死循环。所以就 T 了
|
4
MrXiong OP @zjbztianya 谢谢,明白了
|