[Leetcode] [JavaScript] First Bad Version

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.