leetcode之first bad version
来源:互联网 发布:校园网络管理 编辑:程序博客网 时间:2024/05/15 19:43
题目:
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.
解答:
很明显的binary search注意和普通二分查找的区别,同时注意在求mid的时候尽量写成l + (r - l) / 2的形式,防止溢出
// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: int firstBadVersion(int n) { int l = 1; int r = n; while(l < r) { int mid = l + (r - l)/2; if(isBadVersion(mid)) r = mid; else l = mid + 1; } return l; }};
0 0
- leetcode之First Bad Version
- leetcode之first bad version
- [leetcode]First Bad Version
- LeetCode First Bad Version
- [leetcode] First Bad Version
- [Leetcode]First Bad Version
- Leetcode: First Bad Version
- leetcode First Bad Version
- LeetCode || First Bad Version
- [leetcode]First Bad Version
- Leetcode: First Bad Version
- #leetcode#First Bad Version
- leetcode:First Bad Version
- LeetCode-First Bad Version
- [LeetCode]First Bad Version
- [LeetCode]First Bad Version
- leetcode-First Bad Version
- [LeetCode]First Bad Version
- iOS开发之支付宝集成
- 对象等同性
- Remote shell with telnet on windows platform
- 联芯代码如何去掉QuickSetting下蓝牙及wifi快捷图标
- vnc 全屏设置 window和ubuntu客户端的使用
- leetcode之first bad version
- WPF 按钮背景图片替换设置
- JSON数据格式
- Random intersection
- 作业day07
- MOOC Machine Learning 作业交流帖7
- 构造函数 和 析构函数
- (GOF23设计模式)_单例模式_应用场景_饿汉式_懒汉式
- No Launcher activity found!The launch will only sync the application package on the device!