-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
37 lines (36 loc) · 840 Bytes
/
solution.js
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
/**
* // This is the ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* function ArrayReader() {
*
* @param {number} index
* @return {number}
* this.get = function(index) {
* ...
* };
* };
*/
/**
* @param {ArrayReader} reader
* @param {number} target
* @return {number}
*/
var search = function (reader, target) {
const OVER = 2147483647;
let low = 0;
let high = 20000;
while (low <= high) {
const mid = low + ((high - low) >> 1);
const rst = reader.get(mid);
if (rst === OVER) {
high = mid - 1;
} else if (rst > target) {
high = mid - 1;
} else if (rst < target) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
};