LeetCode 278. First Bad Version
2016-03-07 15:23
393 查看
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.
分析:本题大意就是找到第一个使isBadVersion(version)
返回为false的索引值。本题与(M) Search
Insert Position 类似
题目要求减少调用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.
分析:本题大意就是找到第一个使isBadVersion(version)
返回为false的索引值。本题与(M) Search
Insert Position 类似
题目要求减少调用API的次数。可以选择二叉查找法。
代码:
/* 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 l = 1, r = n; int m; while(l < r){ m = l + (r - l) / 2;// 二叉查找 boolean f1 = isBadVersion(m); if(f1) r = m;// 若当前 m 对应为假,则记录下来 else l = m + 1;// 否则循环判断下半区 }// 循环结束,r 一定指向第一个badVersion return r; } }
相关文章推荐
- CSDN博客积分规则和获取积分方法
- 关于QT的QProcess运行外部程序的问题
- 主成分分析(Principal components analysis)-最大方差解释
- JAVA实现在excel插入图片(POI模式)
- python集成开发环境PyCharm5.04注册以及简单使用
- C++服务器端+Android客户端的网络通信
- 装B失败小记一则
- jQuery对下拉框Select操作总结
- C语言深度剖析-----函数
- React Native-18.React Native 常用API及实践 NetINfo
- iOS开发--应用程序上线
- 对原型的个人理解
- 常用的正则表达式
- 关于android应用程序的入口
- Ios设计模式
- 10032---Node.js实战--欢迎进入Node.js世界
- 使用命令给APK进行签名
- 简单工厂模式
- 加载SD卡图片到Gallery与ImageSwitch使用详解
- boost 各个模块的功能