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. ...
随机推荐
- ostream类重载的operator<<()函数
ostream类重载了operator<<()以识别不同的类型,如: int short long unsigned int unsigned short unsigned long f ...
- valueOf跟toString区别
1.用法如下:toString()方法:返回对象的字符串表示. 对象 操作 Array 将 Array 的元素转换为字符串.结果字符串由逗号分隔,且连接起来. Boolean 如果 Boolean 值 ...
- 委托、匿名委托和lambda表达式
1.委托 在.NET中,委托有点类似于C/C++中的函数指针,但与指针不同的是,委托是一种安全的类型,那么我们就以实现两个数的差为例,先声明一个成员方法: public int CompareTwoV ...
- myBatis自动生成相关代码文件配置(Maven)
pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...
- Netsharp快速入门(之11) 销售管理(开发销售订单工作区)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 4.3 销售订单开发 4.3.1 部件工作区设置 1.创建部件工作区,建工作区向导中要注意勾选组合并系部分.具体要建立的部 ...
- Java多线程时内存模型
1. 概述 多任务和高并发是衡量一台计算机处理器的能力重要指标之一.一般衡量一个服务器性能的高低好坏,使用每秒事务处理数(Transactions Per Second,TPS)这个指标比较能说明问题 ...
- Java 7 中 NIO.2 的使用——第一节 Path 类的使用
路径隶属于文件系统,实际上它是存储和组织媒体文件的格式,通常在一块或多块硬盘设备上,以便于非常容易地检索.文件系统可以通过 java.nio.file.FileSystems 这个final 类来访 ...
- JS中函数的基础知识
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- 响应式嵌入 iframe Pym.js
Pym.js 可以让你在嵌入 iframe 的时候可自动的对 iframe 的大小进行调整以适应父一层容器,并且可以避免跨域问题. 支持浏览器: Internet Explorer 9, 10 (Wi ...
- VSS
A deleted file of the same name already exists in this VSS project. Do you want to recover the delet ...