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) {
if(isBadVersion(1)) return 1;
int left = 1;
int right = n;
int mid=0;
while(left <= right) {
mid = left+(right-left) / 2;
if(isBadVersion(mid)) {
if(!isBadVersion(mid-1))
return mid;
else
right=mid-1;
}
else {
left = mid + 1;
}
}
return -1;
} }

  

 运行结果如下:

 

(medium)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. Leetcode 278 First Bad Version 二分查找(二分下标)

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

  4. Java [Leetcode 278]First Bad Version

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

  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. 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导致的溢 ...

  8. 【leetcode】278. First Bad Version

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

  9. 278. First Bad Version - LeetCode

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

随机推荐

  1. select document library from certain list 分类: Sharepoint 2015-07-05 07:52 6人阅读 评论(0) 收藏

    S using System; using Microsoft.SharePoint; namespace Test { class ConsoleApp { static void Main(str ...

  2. qpython3 读取安卓lastpass Cookies

    之前我的博客写了python读取windows chrome Cookies,沿着同样的思路,这次本来想尝试读取安卓chrome Cookies, 但是可能是chrome的sqlite3版本比较高读取 ...

  3. 数组第K小数问题 及其对于 快排和堆排 的相关优化比较

    题目描述 给定一个整数数组a[0,...,n-1],求数组中第k小数 输入描述 首先输入数组长度n和k,其中1<=n<=5000, 1<=k<=n 然后输出n个整形元素,每个数 ...

  4. Java技术的特点

    Java技术是一套完整的IT行业解决方案,其中包含了很多技术.最初是从解决家电设备联网通讯的方案发展起来的,其特点适用于Internet,于是在Internet广泛应用的环境下,迅速发展成为一种计算机 ...

  5. linux 远程管理

    启动linuxssh 服务: /etc/init.d/ssh 启动网络服务: service network restart linux远程登录配置过程: 首先在ubuntu下安装openssh-se ...

  6. (实用篇)PHP递归实现无限级分类

    在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类. 在一些复杂的系统中,要求对信 ...

  7. 更新EF,EF 报错

    在项目中,对一个视图进行了更新,增加了一个字段,然后需要更新EF访问,可是往往会报错, 查看映射关系发现EF将字段映射为主键,而视图没有进行ISNULL处理. 可以有两种处理方式: 1:修改视图对字段 ...

  8. Windows 下的 Sublime Text 2 配置汇总, Sublime Text 3 亦可借鉴

    1)软件下载地址:http://www.sublimetext.com/2 2)安装 Package Control ,方便安装和管理插件,网络资源很多,这里附上一篇:http://www.imjef ...

  9. ubuntu QWT Qt

    1,下载QWT 2,解压进入QWT的目录 3,qmake,生成makefile文件 4,编译,make 5,安装,make install ,需要root 安装好后会在 /usr/local 目录下有 ...

  10. iOS红马甲项目Bug总结(3)

    这里是一些小总结 1.使用图片缓存之后,新添加的图像一直不能显示 2.项目打包通过appliction loader上传成功了,可是itunes 上面的构建版本项,一直没显示出来 3.界面加载之后,t ...