`

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.

用二分法,设定两个指针,分别指向数组的头和尾。如果中间的version是坏的,说明坏的version在左边,就让右指针左移;如果中间的version是好的,说明坏的version在右边,让左指针右移。代码如下:
/* 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 l = 1;
        int r = n;
        while(l <= r) {
            int m = l + (r - l) / 2;
            if(isBadVersion(m))
                r = m - 1;
            else
                l = m + 1;
        }
        return l;
    }
}
0
3
分享到:
评论

相关推荐

    Leetcode May Challenge – 05/01: First Bad Version(Python)

    在LeetCode五月挑战的首日问题“First Bad Version”中,你被设定为一名产品经理,你的团队正在开发一款新产品。不幸的是,最新的版本未能通过质量检查。由于每个新版本都是基于旧版本构建的,一旦有一个版本出现...

    五月算法精选合集.pdf

    在文档中提到的First Bad Version问题里,应用二分查找法可以达到时间复杂度O(logn),空间复杂度O(1)。这是因为二分查找不需要额外的存储空间,只需用到常数级别的变量来帮助定位。 #### 1.3 二分查找模板 文档提供...

    手稿_V1.113

    问题来源于LeetCode的一个经典题目,名为“First Bad Version”。该问题的目标是找到一系列版本中的第一个错误版本,其中版本是从1到n编号的。我们可以调用一个接口`isBadVersion(version)`来判断给定的版本号`...

    算法刷题笔记leetcode/lintcode

    - First Bad Version(第一个错误版本) - Search a 2D Matrix(二维矩阵中的搜索) - Search a 2D Matrix II(二维矩阵中的搜索II) - Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array...

    lintcode算法分析和解答

    - **第一个错误版本(First Bad Version)** - 在一系列版本中找到第一个错误版本。 - **二维矩阵查找(Search a 2D Matrix)** - 在一个按行、列都升序排列的二维矩阵中查找一个元素。 - **查找峰值元素(Find Peak ...

    lrucacheleetcode-Coding-Interview:编程面试

    lru cache leetcode Coding-Interview A repo for popular ...Version Valid Perfect Square 二叉树查找/BST查找 Closest Binary Search Tree Value 二叉树查找/二叉树第K个 Kth Smallest Element In A

    leetcode卡-leetcode:利特码解决方案

    Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: 基础技巧:分治、二分、贪心 排序算法:快速排序、归并排序、计数排序 搜索...

    leetcode答案-Conquer-Leetcode:征服-Leetcode

    Version 找target本尊 target若有也只有一个 但这道题api接口只返回两种情况 Search Insert Position 找target能插入的第一个位置 或 比target小的值有几个 H-Index II 注意是找后面的target 而且是动态target sqrtx...

    LeetCode判断字符串是否循环-May-LeetCode-Challenge:31天五月LeetCode挑战赛

    LeetCode判断字符串是否循环五月力扣挑战赛 第 01 天。 描述 : ...first bad one, which causes all the following ones to be bad. You are given an API bool isBadVersion(version) which will

    Linux System Administrator Guide Version0.9

    - **Checking for Disk Errors with badblocks**: Explains using `badblocks` to find and mark bad sectors. - **Fighting Fragmentation?**: Offers strategies to combat fragmentation. - **Other Tools for...

    Android代码-Ultrasonic

    Ultrasonic is free and open-source music streaming Android client for Subsonic API (version 1.7.0 or higher) compatible servers. Download App is available to download at following stores: Bugs and ...

    Methods of Mathematical Physics Vol I 1937

    First, may be the original version is in German, so even with good translation, it seem does not fit in the usual English style we get used to. Also the topics it choose is too few and also the area ...

    The Art of SQL

    A database administrator tries to get the most out of a systema given hardware, processors and storage subsystem, or a given version of the database. A database administrator may have some SQL skills...

    BobBuilder_app

    As you can see the page list is a sorted dictionary of first keys from each page along with associated page number and page items count. A page is a dictionary of key and record number pairs. This ...

    ssd3选择题答案大全

    - (a) an @version tag - (b) the order of lines is not important - (c) an @author tag - (d) a summary sentence of the declared entry - **正确答案**:(d) a summary sentence of the declared entry - *...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00331576 Defect LSI MegaCLI EncInfo (Enclosure Info) duplicates the first power supply status data to both power supplies SCGCQ00333070 Defect make files updates SCGCQ00333590 Defect MegaCLI 8.04...

    BURNINTEST--硬件检测工具

    - BurnInTest could have crashed on accessing bad video memory hardware in the 2D test. This problem is now just reported as an error (and BurnInTest) continues. - When BurnInTest crashes, it should...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    RSP-20372 A generic "reference to function" will only match the first of several overloaded functions RSP-19714 Win32 compiler - Memory corruption with array helpers RSP-18241 *.c source files, added ...

    CSerialPort串口类最新修正版2016-08-02

    First Version by Remon Spekreijse on 2000-02-08 http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2483/A-communication-class-for-serial-port.htm Second Version by mrlong on ...

Global site tag (gtag.js) - Google Analytics