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.

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.

 

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

class Solution {
public:
    int firstBadVersion(int n) {
        int left=1;
        int right=n;
        while(left<=right)
        {
            int mid=left+((right-left)>>1);
            if(isBadVersion(mid))
            {
                if(mid==1||mid>1&&!isBadVersion(mid-1))
                    return mid;
                right=mid-1;
            }
            else
                left=mid+1;
        }
        return -1;
    }
};

 

相关文章
libtool: Version mismatch error 解决
libtool: Version mismatch error 解决
739 0
|
4月前
|
Python
ERROR: Could not find a version that satisfies the requirement thop (from versions: none) ERROR: No
这篇文章介绍了在尝试安装`thop`包时遇到的"No matching distribution found"错误,并提供了通过直接从GitHub源码安装`thop`的解决方法。
ERROR: Could not find a version that satisfies the requirement thop (from versions: none) ERROR: No
|
7月前
|
SQL 流计算
"Missing version in readMessageBegin, old client?
"Missing version in readMessageBegin, old client?
80 1
|
7月前
|
Java
Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
146 0
|
7月前
|
SQL 流计算
Missing version in readMessageBegin, old client?"这个错误
Missing version in readMessageBegin, old client?"这个错误【1月更文挑战第22天】【1月更文挑战第107篇】
212 1
AttributeError: ‘version_info‘ object has no attribute ‘version‘
AttributeError: ‘version_info‘ object has no attribute ‘version‘
253 0
|
Java 编译器 应用服务中间件
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
164 0
Unsupported major.minor version 52.0(unable to load class com.xxxxxxx.xxx.xx.xx)
|
测试技术 API
LeetCode 278. First Bad Version
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
62 0
LeetCode 278. First Bad Version
|
Java Linux
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
libtool: Version mismatch error
libtool: Version mismatch error
102 0