LEETCODE--First Bad Version
2015-11-23 17:49
274 查看
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.
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.
// Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int end = n; int begin = 1; int mid; while((begin+1) < end){ mid = (begin+end)/2; if(isBadVersion(mid)) end = mid; else begin = mid; } if(isBadVersion(begin)) return begin; else return end; } };
相关文章推荐
- 高精度-HDU-1002-A + B Problem II
- nginx反向代理原理和配置讲解
- android多屏幕适配注意几点
- IE下判断IE版本语法使用
- 锦隆驾校考试场---大路
- android WindowManager
- Android动画的fillAfter属性
- hdu 5570 期望好题
- CYC-关于UIImagePickerController
- mongoose在Windows Server 2003上不能访问问题的解决
- 数据仓库3级范式(3NF)基础
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式
- 设置QQ浏览器中页面的护眼模式