[LeetCode] 278. 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 returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1
Output: 1

Constraints:

  • 1 <= bad <= n <= 231 - 1

找到第一个坏版本。

你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。

假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。

你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/first-bad-version
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二分法的基础题。注意二分找中点的时候记得用Math.floor因为JavaScript没有整型,所以做除法的时候是会给出小数的,这样会导致死循环和超时。

时间O(logn)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {function} isBadVersion()
 3  * @return {function}
 4  */
 5 var solution = function(isBadVersion) {
 6     /**
 7      * @param {integer} n Total versions
 8      * @return {integer} The first bad version
 9      */
10     return function(n) {
11         let start = 1;
12         let end = n;
13         while (start <= end) {
14             let mid = Math.floor(start + (end - start) / 2);
15             if (isBadVersion(mid)) {
16                 end = mid - 1;
17             } else {
18                 start = mid + 1;
19             }
20         }
21         return start;
22     };
23 };

Java实现一,模板二

模板参考这个帖子

 1 public class Solution extends VersionControl {
 2     public int firstBadVersion(int n) {
 3         int start = 1;
 4         int end = n;
 5         while (start <= end) {
 6             int mid = start + (end - start) / 2;
 7             if (isBadVersion(mid)) {
 8                 end = mid - 1;
 9             } else {
10                 start = mid + 1;
11             }
12         }
13         return start;
14     }
15 }

Java实现二,模板三

这里的搜索范围是左闭右开,[start, end)。所以当 isBadVersion(mid) == false的时候,说明当前的mid是一个好的版本,mid = start + 1;但是如果 isBadVersion(mid) == true,说明当前这个mid是一个坏的版本,而又由于end是开区间的关系,所以搜索的范围的上界只能从end移动到mid。

/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int start = 1;
        int end = n;
        while (start < end) {
            int mid = start + (end - start) / 2;
            if (isBadVersion(mid)) {
                end = mid;
            } else {
                start = mid + 1;
            }
        }
        return start;
    }
}

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/11776656.html