Skip to content

Commit 6e1eb5c

Browse files
add js Solution for 278;
1 parent bb30904 commit 6e1eb5c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
const 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 === 1) return n;
22+
let left = 1, right = n;
23+
while(left < right){
24+
let mid = left + Math.floor((right-left)/2);
25+
if(isBadVersion(mid)){
26+
if(mid === left) return mid;
27+
else right = mid;
28+
}else{
29+
if(isBadVersion(mid+1)) return mid+1;
30+
else left = mid + 1;
31+
}
32+
}
33+
};
34+
};

0 commit comments

Comments
 (0)