- 浏览: 184932 次
- 性别:
- 来自: 济南
文章分类
最新评论
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在右边,让左指针右移。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 692Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 858Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
在LeetCode五月挑战的首日问题“First Bad Version”中,你被设定为一名产品经理,你的团队正在开发一款新产品。不幸的是,最新的版本未能通过质量检查。由于每个新版本都是基于旧版本构建的,一旦有一个版本出现...
在文档中提到的First Bad Version问题里,应用二分查找法可以达到时间复杂度O(logn),空间复杂度O(1)。这是因为二分查找不需要额外的存储空间,只需用到常数级别的变量来帮助定位。 #### 1.3 二分查找模板 文档提供...
问题来源于LeetCode的一个经典题目,名为“First Bad Version”。该问题的目标是找到一系列版本中的第一个错误版本,其中版本是从1到n编号的。我们可以调用一个接口`isBadVersion(version)`来判断给定的版本号`...
- First Bad Version(第一个错误版本) - Search a 2D Matrix(二维矩阵中的搜索) - Search a 2D Matrix II(二维矩阵中的搜索II) - Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array...
- **第一个错误版本(First Bad Version)** - 在一系列版本中找到第一个错误版本。 - **二维矩阵查找(Search a 2D Matrix)** - 在一个按行、列都升序排列的二维矩阵中查找一个元素。 - **查找峰值元素(Find Peak ...
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
Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: 基础技巧:分治、二分、贪心 排序算法:快速排序、归并排序、计数排序 搜索...
Version 找target本尊 target若有也只有一个 但这道题api接口只返回两种情况 Search Insert Position 找target能插入的第一个位置 或 比target小的值有几个 H-Index II 注意是找后面的target 而且是动态target sqrtx...
LeetCode判断字符串是否循环五月力扣挑战赛 第 01 天。 描述 : ...first bad one, which causes all the following ones to be bad. You are given an API bool isBadVersion(version) which will
- **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...
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 ...
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 ...
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...
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 ...
- (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 - *...
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 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...
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 ...
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 ...