File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
solution/278.First Bad Version Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments