-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path374.猜数字大小.ts
45 lines (39 loc) · 967 Bytes
/
374.猜数字大小.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
/*
* @lc app=leetcode.cn id=374 lang=typescript
*
* [374] 猜数字大小
*/
// @lc code=start
/**
* Forward declaration of guess API.
* @param {number} 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
* var guess = function(num) {}
*/
// function guess(n: number): number {
// const pick = 2
// if (n === pick) return 0
// else if (pick < n) return -1
// else return 1
// }
// 二分
function guessNumber(n: number): number {
let left = 1
let right = n + 1
while (left < right) {
const mid = Math.floor((right - left) / 2 + left)
if (guess(mid) === 0) return mid
if (guess(mid) === 1) {
// 猜小了,要往大了猜,收缩左边界,下轮向右边界搜索
left = mid + 1
} else {
// 猜大了
right = mid
}
}
return left
}
// @lc code=end
console.log(guessNumber(3))