Skip to content

Commit 10fdc62

Browse files
committed
update: 278
1 parent f57069b commit 10fdc62

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
9090
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [JavaScript](./src/search-a-2d-matrix-ii/res.js)|Medium|
9191
|274|[H-Index](https://leetcode.com/problems/h-index/) | [JavaScript](./src/h-index/res.js)|Medium|
9292
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/) | [JavaScript](./src/h-index-ii/res.js)|Medium|
93+
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/) | [JavaScript](./src/first-bad-version/res.js)|Easy|
9394
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [JavaScript](./src/move-zeroes/res.js)|Easy|
9495
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [JavaScript](./src/find-the-duplicate-number/res.js)|Medium|
9596
|289|[Game of Life](https://leetcode.com/problems/game-of-life/) | [JavaScript](./src/game-of-life/res.js)|Medium|

‎src/first-bad-version/res.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for isBadVersion()
3+
*
4+
* @param {integer} version number
5+
* @return {boolean} whether the version is bad
6+
* isBadVersion = function(version) {
7+
* ...
8+
* };
9+
*/
10+
11+
/**
12+
* @param {function} isBadVersion()
13+
* @return {function}
14+
*/
15+
var solution = function(isBadVersion) {
16+
/**
17+
* @param {integer} n Total versions
18+
* @return {integer} The first bad version
19+
*/
20+
return function(n) {
21+
if (n < 2) return n;
22+
23+
let left = 1, right = n;
24+
25+
while (left < right) {
26+
const mid = parseInt((left + right) / 2);
27+
28+
if(isBadVersion(mid)) { // 在左边
29+
right = mid;
30+
} else { // 在右边
31+
left = mid + 1;
32+
}
33+
}
34+
35+
return right;
36+
};
37+
};

0 commit comments

Comments
 (0)