-
Notifications
You must be signed in to change notification settings - Fork 0
/
374.hpp
44 lines (37 loc) · 870 Bytes
/
374.hpp
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
42
43
44
#ifndef LEETCODE_374_HPP
#define LEETCODE_374_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
// 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 l = 1, h = n + 1;
int mean = 0;
while (true) {
mean = l + (h - l) / 2;
int g = guess(mean);
if (g == 0)
break;
else if (g > 0) {
l = mean + 1;
} else {
h = mean;
}
}
return mean;
}
};
#endif //LEETCODE_374_HPP