LeetCode刷题实战374:猜数字大小
示例
示例 1:
输入:n = 10, pick = 6
输出:6
示例 2:
输入:n = 1, pick = 1
输出:1
示例 3:
输入:n = 2, pick = 1
输出:1
示例 4:
输入:n = 2, pick = 2
输出:2
解题
public class Solution extends GuessGame {
public int guessNumber(int n) {
int left = 1;
int right = n;
while (left <= right){
int mid = left +(right - left)/2;
if(guess(mid) == 1) left = mid+1;
else if(guess(mid) == -1) right = mid - 1;
else return mid;
}
return -1;
}
}
赞 (0)