Skip to content

Commit 2a4d657

Browse files
committed
374. Guess Number Higher or Lower
1 parent 07f9a75 commit 2a4d657

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution extends GuessGame {
2+
public int guessNumber(int n) {
3+
int l = 1;
4+
int r = n;
5+
6+
while (l <= r) {
7+
8+
int mid = l + (r - l) / 2;
9+
int num = guess(mid);
10+
11+
if (num == -1) {
12+
r = mid - 1;
13+
} else if (num == 1) {
14+
l = mid + 1;
15+
} else {
16+
return mid;
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
}

0 commit comments

Comments
 (0)