[LeetCode] 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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

话说这个叫李建超的哥们太赞了,自从LeetCode开始收费后,大多数的免费题都是这哥们出的,还写了所有的test cases,32个赞。这道题说是有一系列版本,其中有一个版本是坏的,而且后面跟着的全是坏的,给了一个API函数可以用来判定当前版本是否是坏的,让我们尽可能少的调用这个API,找出第一个坏版本。那么这种搜索题最先开始考虑用二分查找法把,效率高嘛。由于这题很有规律,好版本和坏版本一定有个边界,那么我们用二分法来找这个边界,对mid值调用API函数,如果是坏版本,说明边界在左边,则把mid赋值给right,如果是好版本,则说明边界在右边,则把mid+1赋给left,最后返回left即可。需要注意的是,OJ里有个坑,那就是如果left和right都特别大的话,那么left+right可能会溢出,我们的处理方法就是变成left + (right - left) / 2,很好的避免的溢出问题,参见代码如下:

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int left = 1, right = n;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (isBadVersion(mid)) right = mid;
            else left = mid + 1;
        }
        return left;
    }
};

如果习惯了二分搜索法从0开始找,可以用下面的方法:

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int left = 0, right = n - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (isBadVersion(mid + 1)) right = mid;
            else left = mid + 1;
        }
        return right + 1;
    }
};

本文转自博客园Grandyang的博客,原文链接:第一个坏版本[LeetCode] First Bad Version ,如需转载请自行联系原博主。

相关文章
|
索引
LeetCode 387. First Unique Character in a String
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
124 0
LeetCode 387. First Unique Character in a String
|
测试技术 API
LeetCode 278. First Bad Version
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
70 0
LeetCode 278. First Bad Version
|
测试技术 API
LeetCode 278. 第一个错误的版本 First Bad Version
LeetCode 278. 第一个错误的版本 First Bad Version
|
数据库
LeetCode(数据库)- First and Last Call On the Same Day
LeetCode(数据库)- First and Last Call On the Same Day
135 0
LeetCode之First Unique Character in a String
LeetCode之First Unique Character in a String
127 0
|
Java 索引 Python
LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母。
786 0
|
算法
LeetCode 41 First Missing Positive(丢失的第一个正数)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52435582 翻译 给定一个未排序的整型数组,找出第一个丢失的正数。
1083 0
LeetCode - 41. First Missing Positive
41. First Missing Positive  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一组整数,找出第一个空缺的正整数.
841 0