-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path374.cpp
More file actions
27 lines (24 loc) · 677 Bytes
/
Copy path374.cpp
File metadata and controls
27 lines (24 loc) · 677 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
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
class Solution {
public:
int guessNumber(int n) {
int left = 1;
int right = n;
while(true){
//int mid = (left + right) / 2;
int mid = (right - left) / 2 + left;
int res = guess(mid);
if(guess(mid) == 0)
return mid;
if(guess(mid) < 0){
right = mid - 1;
}
if(guess(mid) > 0){
left = mid + 1;
}
}
}
};