-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode374.java
More file actions
41 lines (36 loc) · 830 Bytes
/
LeetCode374.java
File metadata and controls
41 lines (36 loc) · 830 Bytes
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
public class LeetCode374 {
public static void main(String[] args) {
System.out.println(new Solution374(1702766719).guessNumber(2126753390));
}
}
class GuessGame {
private int pick;
GuessGame(int pick) {
this.pick = pick;
}
public int guess(int num) {
return pick - num;
}
}
class Solution374 extends GuessGame {
Solution374(int pick) {
super(pick);
}
public int guessNumber(int n) {
int l = 1;
int r = n;
int mid = 1;
while (l <= r) {
mid = (r - l) / 2 + l;
int comp = guess(mid);
if (comp < 0) {
r = mid - 1;
} else if (comp > 0) {
l = mid + 1;
} else {
break;
}
}
return mid;
}
}