278. First Bad Version
2015-12-10 05:45
281 查看
题目:
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
链接: http://leetcode.com/problems/first-bad-version/
题解:
找到First Bad Version,这里我们使用Binary Search就可以了。
Time Complexity - O(logn), Space Complexity - O(1)
二刷:
二分搜索查找bad version的左边界。在找到badversion的时候我们让hi = mid - 1,否则lo = mid + 1, 最后返回lo就是第一个badversion的地方。
Java:
Time Complexity - O(logn), Space Complexity - O(1)
三刷:
Binary Search查找左边界。举个例子 {g, b, b, b, b}就很好理解了。lo和hi相等时hi--,所以我们最后返回lo就可以了
Java:
Reference:
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
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.
链接: http://leetcode.com/problems/first-bad-version/
题解:
找到First Bad Version,这里我们使用Binary Search就可以了。
Time Complexity - O(logn), Space Complexity - O(1)
/* 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 lo = 1, hi = n; while(lo <= hi) { int mid = lo + (hi - lo) / 2; if(isBadVersion(mid)) { hi = mid - 1; } else { lo = mid + 1; } } return lo; } }
二刷:
二分搜索查找bad version的左边界。在找到badversion的时候我们让hi = mid - 1,否则lo = mid + 1, 最后返回lo就是第一个badversion的地方。
Java:
Time Complexity - O(logn), Space Complexity - O(1)
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */ public class Solution extends VersionControl { public int firstBadVersion(int n) { if (n < 1) { return 1; } int lo = 1, hi = n; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (!isBadVersion(mid)) { lo = mid + 1; } else { hi = mid - 1; } } return lo; } }
三刷:
Binary Search查找左边界。举个例子 {g, b, b, b, b}就很好理解了。lo和hi相等时hi--,所以我们最后返回lo就可以了
Java:
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */ public class Solution extends VersionControl { public int firstBadVersion(int n) { if (n < 2) return 1; int lo = 1, hi = n; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (isBadVersion(mid)) hi = mid - 1; else lo = mid + 1; } return lo; } }
Reference:
相关文章推荐
- QT中静态库的生成与使用——创建共享库代码,附例子
- 277. Find the Celebrity
- 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似
- Windows 系统消息范围和前缀,以及消息大全
- SVM在R语言中的应用
- Qt 不规则窗体的实现(构造函数里setPaletteBackgroundPixmap后设置setMask)
- 【Html】Relative与Absolute组合使用
- VC++中的类的内存分布(上)(通过强制转换,观察地址,以及地址里的值来判断)
- 【Html】层模型--固定定位
- 【Html】层模型--相对定位
- hdu 1698 线段树区间修改
- Android RecyclerView使用详解三
- 【Html】层模型--绝对定位
- 【Html】什么是层模型?
- 【Html】浮动模型
- 加黑部分 执行的时候报 IO异常 球大神解答
- C++专题(一)
- 【Html】流动模型(二)
- Eclipse报错 Please ensure that adb is correctly located at '.\adb.exe' and can be
- ObjectAnimator动画使用