Skip to content

Commit 159506f

Browse files
Guess Number Higher or Lower
1 parent 9817496 commit 159506f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Java/374.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Forward declaration of guess API.
3+
* @param num your guess
4+
* @return -1 if num is higher than the picked number
5+
* 1 if num is lower than the picked number
6+
* otherwise return 0
7+
* int guess(int num);
8+
*/
9+
10+
public class Solution extends GuessGame {
11+
public int guessNumber(int n) {
12+
int l = 1, r = n;
13+
while(l<=r) {
14+
int mid = l+(r-l)/2;
15+
if (guess(mid) == 0) {
16+
return mid;
17+
}
18+
else if (guess(mid) == 1) {
19+
l = mid+1;
20+
}
21+
else {
22+
r = mid-1;
23+
}
24+
}
25+
return -1;
26+
}
27+
}

0 commit comments

Comments
 (0)