-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL374.java
More file actions
39 lines (34 loc) · 1.03 KB
/
L374.java
File metadata and controls
39 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.liang.leetcode.binarySearch;
/**
* @ClassName: L374
* @Description: 猜数字大小
* @Author: LiaNg
* @Date: 2020/3/23 7:50
*/
public class L374 {
/**
* 我们正在玩一个猜数字游戏。 游戏规则如下:
* 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。
* 每次你猜错了,我会告诉你这个数字是大了还是小了。
* 你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0):
* -1 : 我的数字比较小
* 1 : 我的数字比较大
* 0 : 恭喜!你猜对了!
*/
public int guessNumber(int n) {
int low = 1;
int high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
int res = 0;//guess(mid);
if (res == 0) {
return mid;
} else if (res < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}