LeetCode 278. First Bad Version
2016-05-09 13:21
531 查看
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
Binary Search of the left most 1.
are 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.
Binary Search of the left most 1.
int firstBadVersion(int n) { int left = 1, right = n; while(left < right) { int mid = left + (right - left) / 2; if(isBadVersion(mid) == 0) left = mid + 1; else right = mid; } if(isBadVersion(left)) return left; return -1; }
相关文章推荐
- C 运算符优先级列表
- Palindromic Squares
- mysql char、varchar
- Dremel和Hadoop
- C语言————常量 、变量、 表达式、 语句详谈
- Yii2.0 RESTful Web服务(4)
- 一站式远程页面调试工具spy-debugger 2.0,已支持HTTPS
- vagrant搭建lamp开发环境
- Android_Studio 及SDK下载
- Hadoop笔记2016-05-09
- JavaScript的获取非行间样式
- java基本排序算法总结 (二)——快速排序法
- HashMap与HashTable的区别?
- 致caozhy
- 数据挖掘与R语言,数据分析,机器学习
- 快速排序
- CSS中的圣杯布局,以及圣杯布局与双飞翼布局的区别
- Eclipse下使用Maven搭建SpringMVC项目
- jpeg了解
- 个人站长不死的其中四个理由