[LeetCode][JavaScript]First Bad Version

First Bad Version

You are 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 is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. 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/



找出第一个bad version,bad version之后的version都是bad,version的范围是正整数,isBadVersion是已经写好的方法,只要调用就行了。

二分法,如果一个数是bad version而它前一个数不是,这个数就是结果,否则就进行二分。

 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  * @param {function} isBadVersion()
12  * @return {function}
13  */
14 var solution = function(isBadVersion) {
15     /**
16      * @param {integer} n Total versions
17      * @return {integer} The first bad version
18      */
19     return function(n) {
20         return findBadVersion(1, n);
21
22         function findBadVersion(start, end){
23             var index = parseInt((start + end) / 2);
24             if(isBadVersion(index)){
25                 if(!isBadVersion(index - 1)){
26                     return index;
27                 }else{
28                     return findBadVersion(start, index - 1);
29                 }
30             }else{
31                 return findBadVersion(index + 1, end);
32             }
33         }
34     };
35 };

调用:

 1 function test(){
 2     var res = solution(
 3         function(version){
 4             if(version >= 5){
 5                 return true;
 6             }else{
 7                 return false;
 8             }
 9         }
10     )(15);
11     console.log(res);
12 };
时间: 2024-12-19 04:31:48

[LeetCode][JavaScript]First Bad Version的相关文章

[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

LeetCode 278 First Bad Version

LeetCode  278 First Bad Version // Forward declaration of isBadVersion API. bool isBadVersion(int version); int firstBadVersion(int n) { int start=1, end=n; while(start < end) { int mid=start+(end-start)/2; if(isBadVersion(mid)) end=mid; else start=m

LeetCode:First Bad Version - 第一个坏版本

1.题目名称 First Bad Version(第一个坏版本) 2.题目地址 https://leetcode.com/problems/first-bad-version/ 3.题目内容 英文: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c

[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version

[Lintcode]74. First Bad Version [Leetcode]278. First Bad Version 本题难度: [Lintcode]Medium/[Leetcode] Topic: Binary Search Description 我的代码 for Leetcode(在Lintcode中因为isBadVersion调用太多次无法通过) # The isBadVersion API is already defined for you. # @param versi

【Leetcode】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

【一天一道LeetCode】#165. Compare Version Numbers.md

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/compare-version-numbers/ Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 <

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o