[LeetCode] 278. First Bad Version 第一个坏版本
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
你是一个产品经理,目前正在带领一个组开发一个新产品。不幸的是,最近的产品版本质量检查失败了。因为每一个版本都是基于前一个版本开发的,所以一个坏版本后面的所有版本也都是坏的。假设有 n 个版本,你想找到第一个坏的版本。给了一个 API 可以检查版本是否是坏的,实施一个函数找到第一个坏的版本,要最小化调用 API 的次数。
解法1:暴力搜索Brute Force。 [Time Limit Exceeded]
解法2:二分法Binary Search。好版本和坏版本一定有个边界,用二分法来找这个边界,对mid值调用API函数,如果是坏版本,说明边界在左边,则把mid赋值给right,如果是好版本,则说明边界在右边,则把mid+1赋给left,最后返回left。
Java: Time: O(n), Space: O(1), [Time Limit Exceeded]
public int firstBadVersion(int n) {
for (int i = 1; i < n; i++) {
if (isBadVersion(i)) {
return i;
}
}
return n;
}
Java: Time: O(logn), Space: O(1)
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;
} else {
left = mid + 1;
}
}
return left;
}
Python: wo
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left < right:
mid = left + (right - left) / 2
if isBadVersion(mid):
right = mid
else:
left = mid + 1 return left
Python:
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left <= right:
mid = left + (right - left) / 2
if isBadVersion(mid):
right = mid - 1
else:
left = mid + 1
return left
C++: from 1 to n
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = 1, right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) right = mid;
else left = mid + 1;
}
return left;
}
};
C++: from 0 to n - 1
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = 0, right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid + 1)) right = mid;
else left = mid + 1;
}
return right + 1;
}
};
类似题目:
[LeetCode] 35. Search Insert Position 搜索插入位置
[LeetCode] 165. Compare Version Numbers 比较版本数
[LeetCode] 374. Guess Number Higher or Lower 猜数字大小
All LeetCode Questions List 题目汇总
[LeetCode] 278. First Bad Version 第一个坏版本的更多相关文章
- [leetcode]278. First Bad Version首个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- 278 First Bad Version 第一个错误的版本
你是产品经理,目前正在领导一个团队开发一个新产品.不幸的是,您的产品的最新版本没有通过质量检查.由于每个版本都是基于之前的版本开发的,所以错误版本之后的所有版本都是不好的.假设你有 n 个版本 [1, ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- LeetCode 278.First Bad Version(E)(P)
题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- python学习类与方法的调用规则
1类方法的特点是类方法不属于任何该类的对象,只属于类本身 2类的静态方法类似于全局函数,因为静态方法既没有实例方法的self参数也没有类方法的cls参数,谁都可以调用 3.实例方法只属于实例,是实例化 ...
- Android init介绍(上)
1. 介绍 init进程是Linux系统第一个用户进程,是Android系统应用程序的根进程,即1号进程(PID为1):Android中的init文件位于/init,代码位于system/core/i ...
- test20190924 老L
80+50+100=230.T1没做出来说明我数列学得不好? LOLO 的含树 现有函数 \[ g_m(i)=\begin{cases} 0, & 0 \leq i \leq m\\ i-1+ ...
- 关于AndroidStudio的apk打包遇到的问题记录
12月份末尾,想来个总结,主要是得记一些重要的. 首先就得是AndroidStudi内的apk打包,就是弄当前项目app的安装包出来. 下面就说下具体步骤和注意问题. 首先 : 看到AndroidSt ...
- centos7.2(二)搭建lamp(Apache+PHP+Mysql环境)教程
开始安装前,看说明. 说明0 查看服务器是否能被ssh登陆 http://tool.chinaz.com/port/ 如果显示关闭,说明被大陆封闭了,删除服务器重新建立一个. 说明1:Centos7 ...
- 使用docker来创建一个etcd集群
docker run -d --name etcd1 --network etcdnet --ip 172.25.0.101 -p 23791:2379 -e ETCDCTL_API=3 -v /ro ...
- SpringCloud组件Eureka
什么是微服务架构 架构设计概念,各服务间隔离(分布式也是隔离),自治(分布式依赖整体组合)其它特性(单一职责,边界,异步通信,独立部署)是分布式概念的跟严格执行SOA到微服务架构的演进过程作用:各服务 ...
- 利用dnslog进行sql注入
更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 能够利用dnslog进行注入的两个关键点:secure_file_priv 不能为NULL,仅支持 Windows但不支持 Linux s ...
- 洛谷 P1250 种树 题解
差分约束系统,维护前缀和,根据式子d[ b ] < = d[ e + 1 ] - t,可以看出要连e和b - 1,但占用了超级源点0,所以要把区间向后移,这样就可以用超级源点0来保持图的连通性( ...
- java 数组逆序输出(方法内部的代码)
//现在数组中有1, 2, 4, 5, 6, 7, 8 请逆序输出 int [] arrs={1,2,3,4,5,6,7,8}; for(int i=arrs.length-1;i>-1;i-- ...