-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSolution.java
More file actions
51 lines (45 loc) · 1.31 KB
/
Solution.java
File metadata and controls
51 lines (45 loc) · 1.31 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
40
41
42
43
44
45
46
47
48
49
50
51
package ds.binary.leetcode374;
/**
* 猜数字大小
* LeetCode 374. https://leetcode.cn/problems/guess-number-higher-or-lower/
*
* @author yangyi 2022年07月12日15:04:14
*/
public class Solution {
private final int pick;
public Solution(int pick) {
this.pick = pick;
}
public int guessNumber(int num) {
int start = 0, end = num;
while (start <= end) {
int middle = start + (end - start) / 2;
if (guess(middle) == 1) {
start = middle + 1;
} else if (guess(middle) == -1) {
end = middle - 1;
} else {
return middle;
}
}
return -1;
}
/**
* 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);
*/
private int guess(int num) {
return Integer.compare(pick, num);
}
public static void main(String[] args) {
System.out.println(new Solution(6).guessNumber(10));
System.out.println(new Solution(1).guessNumber(1));
System.out.println(new Solution(1).guessNumber(2));
System.out.println(new Solution(2).guessNumber(2));
}
}