-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
47 lines (39 loc) · 888 Bytes
/
solution.ts
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
45
46
47
/*
* @lc app=leetcode id=374 lang=javascript
*
* [374] Guess Number Higher or Lower
*/
// @lc code=start
/**
* Forward declaration of guess API.
* @param {number} num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* var guess = function(num) {}
*/
/**
* @param {number} n
* @return {number}
*/
const guessNumber = (n: number): number => {
// * ['52 ms', '73.49 %', '33.6 MB', '100 %']
let left = 0;
let right = n;
let mid = 0;
while (left <= right) {
mid = ~~((left + right) / 2);
// @ts-ignore
const check = guess(mid);
if (check === 1) {
left = mid + 1;
} else if (check === -1) {
right = mid - 1;
} else {
break;
}
}
return mid;
};
// @lc code=end
export { guessNumber };