374. 猜数字大小
小于 1 分钟
374. 猜数字大小简单
解法思路:二分查找
public class Solution extends GuessGame {
public int guessNumber(int n) {
long left = 1;
long right = n;
while(guess((int)((left + right)/2)) != 0){
if(guess((int)((left + right)/2)) < 0) {
right = (left + right)/2 - 1;
}
if(guess((int)((left + right)/2)) > 0) {
left = (left + right)/2 + 1;
}
}
return (int) ((left + right)/2);
}
}
function guessNumber(n: number): number {
let left:number = 1;
let right:number = n;
while(guess((left+right)/2) !== 0){
if(guess((left + right)/2) < 0) {
right = (left + right)/2 - 1;
}
if(guess((left + right)/2) > 0) {
left = (left + right)/2 + 1;
}
}
return ((left + right)/2);
};
Powered by Waline v2.15.5