First Bad Version
2015-10-20 21:27
309 查看
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
You are given an API
bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
also bad.
Suppose you have
nversions
[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
versionis
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 (n == 0) { return -1; } int start = 1, end = n, mid; while (start + 1 < end) { mid = start + (end - start)/2; if (isBadVersion(mid) == false) { start = mid; } else { end = mid; } } if (isBadVersion(start) == true) { return start; } else if (isBadVersion(end) == true) { return end; } else { return -1; // not found } } }
相关文章推荐
- Linux下用SCP无需输入密码传输文件
- Android开发手记(13) 几种Alertdialog的使用
- 面试题37:两个链表的第一个公共结点
- UVa10895 Placing Lampposts
- Windows下配置php环境
- 内核线程&&系统调用exit&&wait4&&撤销进程
- zigbee协议栈学习(三)
- XML创建数据表
- HEVC中CU和TU的划分的过程
- dom4j XMLWriter 设置xml文件为UTF-8
- 您可能不曾注意的C++内置类型选择和使用的注意事项
- iWebShop -- 代替ecshop 的开源电子商务网站系统
- Mac下Tomcat的下载安装和配置
- 黑马程序员--【阳哥笔记】Java基础之笔记系列(全)
- Linux下的网络编程模型总结
- 输入一个链表,从尾到头打印链表每个节点的值
- JBOSS.71.1.Final安装配置
- ubuntux下apk反编译工具安装
- Java反射实例-Reflect
- Java 中关于原码,反码,补码的问题及常用的逻辑运算