-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (29 loc) · 788 Bytes
/
Copy pathSolution.java
File metadata and controls
32 lines (29 loc) · 788 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
/*
* @lc app=leetcode id=374 lang=java
*
* [374] Guess Number Higher or Lower
*/
// @lc code=start
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/
public class Solution extends GuessGame {
public int guessNumber(int n) {
int lower = 1;
int upper = n;
while (lower <= upper){
int mid = lower + (upper - lower) / 2;
int guessResult = guess(mid);
if (guessResult == 0) return mid;
else if (guessResult < 0) upper = mid - 1;
else lower = mid + 1;
}
return -1;
}
}
// @lc code=end