[leetcode]278. 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.
Example:
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

思路
binary search
代码
/* 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) {
// avoid overflow
int mid = start + (end-start) / 2;
if (!isBadVersion(mid)){
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
[leetcode]278. First Bad Version首个坏版本的更多相关文章
- [LeetCode] 278. First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- LeetCode OJ:First Bad Version(首个坏版本)
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- LeetCode 278.First Bad Version(E)(P)
题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- gearman在虚拟机上运行没有自动开启的处理
几天来被gearman无响应的问题困扰,后来请教了大神解决.原因是gearman在虚拟机上运行不稳定,无法自动开启,需手动开始,输入图片的两行命令即开始.
- sklearn.svm.SVC 参数说明
原文地址:sklearn.svm.SVC 参数说明 ============================== 资源: sklearn官网+DOC 库下载GitHub =============== ...
- ZooKeeper系列(3)命令操作 (转)
原文地址:http://www.cnblogs.com/wuxl360/p/5817524.html 一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多 ...
- 【Linux_Unix系统编程】chapter5 深入探究文件IO
Chapter5 深入探究文件I/O 本章节将介绍另一个与文件操作相关的系统调用:多用途的fcntl(),并展示其应用之一读取和设置打开文件的状态标志. 5.1 原子操作和竞争条件 所有系统调用都是以 ...
- 小朋友学Java(2):Win 7安装JDK
1 打开命令行窗口,输入java -version. 若提示不认识java命令,说明没有java环境. 1.png 2 从甲骨文网站(http://www.oracle.com/technetwo ...
- 关于raid5的一系列问题
前几天我的一个同事在对计划采购的存储进行测试,期间聊到了raid5的话题,我和他的意见产生了分歧.他的说法是raid5不能挂太多盘是因为如果挂太多盘写惩罚会非常严重导致性能下降.而我的观点则是对于ra ...
- CSS宽度高度的百分比取值基于谁
width=num% , height=num% 基于以下几点 1. 若元素不存在定位: 则基于直接父元素的宽高度 2. 若元素存在定位 且 定位为 relative, 则也基于直接父元素的宽高度 3 ...
- Python - Django - ORM 操作表
ORM 的对应关系: 类 ---> 数据库表对象 ---> 数据库行属性 ---> 字段 操作数据库表 ---> ...
- bat批处理(二):%0 %1——给批处理脚本传递参数
初次接触批处理脚本觉得有点意思,所以决定写一个小功能试验一下,谁知刚一开始就发现遇到了麻烦,本想着使用参数来控制程序的运行结果,可是参数怎么传进去呢,于是研究了一番,最终发现这个参数的传递与main函 ...
- Tensorflow线程和队列
读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...