First Bad Version
You is a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since Each version was developed based on the previous version and all of the versions after a bad version were also bad.
Suppose you n has versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to B E bad.
Given an API which would bool isBadVersion(version) return whether is bad version . Implement a function to find the first bad version. You should minimize the number of calls to the API.
https://leetcode.com/problems/first-bad-version/
Find out that the first bad Version,bad version of version after the Bad,version range is a positive integer, Isbadversion is already written method, as long as the call on the line.
Dichotomy, if a number is bad version and its previous number is not, this number is the result, otherwise it will be two points.
1 /**2 * Definition for isbadversion ()3 * 4 * @param {integer} version number5 * @return {Boolean} whether the version is bad6 * isbadversion = function (version) {7 * ...8 * };9 */Ten /** One * @param {function} isbadversion () A * @return {function} - */ - varSolution =function(isbadversion) { the /** - * @param {integer} n total versions - * @return {integer} the first bad version - */ + return function(n) { - returnFindbadversion (1, n); + A functionfindbadversion (start, end) { at varindex = parseint (start + end)/2); - if(isbadversion (index)) { - if(!isbadversion (index-1)){ - returnindex; -}Else{ - returnFindbadversion (Start, index-1); in } -}Else{ to returnFindbadversion (Index + 1, end); + } - } the }; *};
Call:
1 functionTest () {2 varres =Solution (3 function(version) {4 if(Version >= 5){5 return true;6}Else{7 return false;8 }9 }Ten) (15); One Console.log (res); A};
[Leetcode] [JavaScript] First Bad Version