-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path374.猜数字大小.java
63 lines (60 loc) · 1.46 KB
/
374.猜数字大小.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @lc app=leetcode.cn id=374 lang=java
*
* [374] 猜数字大小
*
* https://leetcode-cn.com/problems/guess-number-higher-or-lower/description/
*
* algorithms
* Easy (44.17%)
* Likes: 64
* Dislikes: 0
* Total Accepted: 25.7K
* Total Submissions: 57.5K
* Testcase Example: '10\n6'
*
* 我们正在玩一个猜数字游戏。 游戏规则如下:
* 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。
* 每次你猜错了,我会告诉你这个数字是大了还是小了。
* 你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0):
*
* -1 : 我的数字比较小
* 1 : 我的数字比较大
* 0 : 恭喜!你猜对了!
*
*
*
*
* 示例 :
*
* 输入: n = 10, pick = 6
* 输出: 6
*
*/
// @lc code=start
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* int guess(int num);
*/
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) {
right = mid - 1;
} else if (guess(mid) == 1) {
left = mid + 1;
} else if (guess(mid) == 0) {
return mid;
}
}
return -1;
}
}
// @lc code=end