Leetcode67: First Bad Version
2015-09-20 16:57
204 查看
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.
// Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int left = 1; int right = n; while(left <= right) { int mid = left + (right - left)/2; if(isBadVersion(mid)) { right = mid - 1; } else { left = mid + 1; } } return left; } };
二分查找法即可。
相关文章推荐
- springsource tool suite下载地址分享
- 当今世界最NB的25位大数据科学家
- Python中super的用法
- 校招回忆录
- 机房收费系统(合作版)总结——技术篇(二)
- “软件工程”
- 5种服务器网络编程模型讲解
- 你怎么看?
- java匿名内部类总结
- TextView文字段插入图片和点击
- Symmetric Tree
- Estimation of Restored MB by Temporal Error Concealment
- svm评价指标公式
- 项目19.2 能够对齐的数据
- Android JNI环境要SQLite加密模块简介
- [LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串
- C基础03天--循环控制语句笔记
- UML——宏观把控(二)
- MATLAB日常学习记录
- soj.1006 Team Rankings