题目描述:

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.

解题思路:

使用二分法查找。同时注意二分法查找时候中间位置的数值不能溢出。

代码如下:

/* 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, end = n;
while(start < end){
int mid = start + (end - start) / 2;
if(isBadVersion(mid))
end = mid;
else
start = mid + 1;
}
return start;
}
}

  

Java [Leetcode 278]First Bad Version的更多相关文章

  1. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  2. leetcode 278. First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  3. (medium)LeetCode 278.First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  5. [leetcode]278. First Bad Version首个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  6. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  7. 278. First Bad Version - LeetCode

    Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...

  8. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  9. 【leetcode】278. First Bad Version

    problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...

随机推荐

  1. JS实现Web网页打印功能(IE)

    问题描述:     JS实现Web网页打印功能 问题解决:     这里主要使用WebBrowser控件的ExeWB在IE中打印功能的实现 WebBrowser介绍:         WebBrows ...

  2. JUnit4的使用

    JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写. 先简单解释一下什么是Annotation,这个单词一般是翻译成元数据.元数据 ...

  3. 奇异值分解(We Recommend a Singular Value Decomposition)

    奇异值分解(We Recommend a Singular Value Decomposition) 原文作者:David Austin原文链接: http://www.ams.org/samplin ...

  4. vs2010中臃肿的ipch和sdf文件

    使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...

  5. 01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之缓存管理

    http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html 管理NHibernate二级缓存 NHibernate二级缓存由ISessionF ...

  6. codeforces 397B

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  7. android 关于InputDispatcher出现Consumer错误的解决办法

    原地址:http://www.educity.cn/wenda/158744.html android 关于InputDispatcher出现Consumer异常的解决方法10-23 03:24:46 ...

  8. PHPer 为什么会被 Javaer 鄙视?

    最近看了知乎上的一个话题 「在工作中,为什么 Java 程序员常常瞧不起 PHP 程序员?」 个人从业多年,用过的后端语言 ASP.ASP.NET.Java.PHP.Node.js.Python,如果 ...

  9. 从后端到页面:如何全方位监控 Ruby 应用?

    [编者按]本文参考技术分享 ,由 OneAPM 工程师补充整理,并且已经征得原作者的同意. 为什么选择 OneAPM ? 在性能监控领域,业界比较有名的是 New Relic 还有 Appdynami ...

  10. poj 3620 Avoid The Lakes(广搜,简单)

    题目 找最大的一片湖的面积,4便有1边相连算相连,4角不算. runtime error 有一种可能是 数组开的太小,越界了 #define _CRT_SECURE_NO_WARNINGS #incl ...