-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstBadVersion.js
More file actions
49 lines (40 loc) · 1.27 KB
/
Copy pathfirstBadVersion.js
File metadata and controls
49 lines (40 loc) · 1.27 KB
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
48
49
// Description
// The code base version is an integer start from 1 to n.One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests.Find the first bad version.
// You can call isBadVersion to help you determine which version is the first bad one.The details interface can be found in the code's annotation part.
// Example:
// Given n = 5, and version = 4 is the first bad version.
// call isBadVersion(3) -> false
// call isBadVersion(5) -> true
// call isBadVersion(4) -> true
// 1 2 3 4 5 6
// O O O X X X
// Then 4 is the first bad version.
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
const solution = isBadVersion => n => {
let start = 1, end = n, mid
while (start + 1 < end) {
mid = start + Math.floor((end - start) / 2)
if (isBadVersion(mid)) {
end = mid
} else {
start = mid
}
}
if (isBadVersion(start)) return start
// if (isBadVersion(end)) return end
return end // 保证有解
}
// 二刷
/*
tips:
1 错误点:审题。是 find the fisrt TRUE. isBadVersion 返回值为true的。
2
const solution = isBadVersion => n => {} 相当于
var solution = function(isBadVersion) {
return function (n) {
};
};
*/