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.
链接: 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;
}
}
Update:
/* 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 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;
}
}
Reference:
278. First Bad Version的更多相关文章
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- 【leetcode】278. First Bad Version
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- 【leetcode❤python】 278. First Bad Version
#-*- coding: UTF-8 -*-# The isBadVersion API is already defined for you.# @param version, an integer ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)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. ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 【easy】278. First Bad Version
有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品. 正确答案: // Forward declaration of isBadVersion API. ...
随机推荐
- Log4Net学习【三】
Log4Net配置详解 配置方式一 在相应的应用程序的配置文件中配置,(WinForm对应的是*.exe.config,WebForm对应的是*.config),本实例是Web应用程序,以Web.co ...
- VBS基础篇 - 内置函数
Date/Time 函数 函数 描述 CDate 把有效的日期和时间表达式转换为日期(Date)类型. Date 返回当前的系统日期. DateAdd 返回已添加指定时间间隔的日期. DateDiff ...
- “我爱淘”冲刺阶段Scrum站立会议1
昨天是我们项目冲刺阶段的第一天,站立会议的内容如下: 1.昨天完成了项目中的第一个界面--“精选”界面:完成了一点Java文件的编写: 2.今天的任务就是完成第一个Activity的编写:将布局文件和 ...
- nscd
作用: Nscd is a daemon that provides a cache for the most common name service requests 可以缓存passwd,grou ...
- Gulp压缩JavaScript代码
因为gulp是自动化工具,所以我们得告诉它,需要怎么做,才能达到我们的目的. 我们首先得新建一个js文件,用来编写我们的需求,以便gulp能按着我们的意愿来执行. 我将这个js文件取名叫gulpfil ...
- 数码管字符产生器GenSym 1.0发布
本软件可以实现以下功能: 1.支持共阴极和共阳极数码管的字符代码的生成. 2.支持C语言和ASM语言方式产生字符串代码的序列. 3.可定制数码管的最高位和最低位的代码产生次序. 4.支持记忆功能,可以 ...
- hibernate学习笔记--可选的配置属性
3.4. 可选的配置属性 有大量属性能用来控制Hibernate在运行期的行为. 它们都是可选的, 并拥有适当的默认值. 警告: 其中一些属性是"系统级(system-level)的&qu ...
- Leetcode#140 Word Break II
原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...
- nsight 使用问题
slot 0 offset 0 stride DXGI_FORMAT_r32b32g32_FLOAT 这样一个memory 100.0000, 100.0000,10.0000,1.0000 stri ...
- 用npm安装express后express命令找不到
Windows 平台加了 npm install -g express 也不行AppData\Roaming\npm 下面没有 express.bat 解决办法: sudo npm install - ...