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. ...
随机推荐
- VC++ MFC 如何实现在编辑框中输出具有换行功能的文段 01
很久不来写东西了,昨天睡觉前写个小工具,突然,这玩意不会换行怎么整... 首先是第一步,获取字符串的长度,转载自白乔的文章. ------------------------------------- ...
- 将项目初始化到git服务器
使用的是GitLab来管理Git服务器; 步骤: 一. 先在服务器上创建一个新的项目(GitLab右上角的New project)
- word2007中如何隐藏工具栏
1.对于屏幕较小的用户来说,编辑时可能需要隐藏word上方的工具栏,具体操作如下:
- 如何查看系统内置的shell命令及帮助信息
1.打开终端,输入help命令可以查看系统默认的shell(通常是bash)的内置的shell命令列表: [tansheng@localhost ~]$ help GNU bash, version ...
- android数独游戏
最近没事干,照着视频教程写了一个数独游戏,很粗糙还有很多要修改的地方.下面就来说说这个游戏吧 1.自定义一个View控件,用来在屏幕上显示一个9*9的格子,其实就是横着画8条线,竖着画8跳线,然后将其 ...
- xml之基础了解
1.简介 1>什么XML语言(eXtensible Markup Language) 可扩展标记语言XML是SGML的子集,其目标是允许普通的SGML在Web上以目前HTML的方式被服务.接受和 ...
- 2014103《JAVA程序设计》第一周学习总结
本周,在刻苦看了三天课本之后,终于对JAVA这门课程有了一定的认识.了解了JAVA的前世今生,JAVA的三大平台:Java SE.Java EE与Java ME.其中Java SE又可分为四个主要的部 ...
- using详解(C#)
using肯定所有人都用过,最简单的就是使用using引入命名空间,然后就是引入别名,简化输入,本文主要介绍第三种用法,即用using强制对象清理资源. 先看下面这段代码: try { using ( ...
- C# Redis
概念 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string( ...
- [Shoi2007]Bookcase 书柜的尺寸 dp
这道dp算是同类型dp中比较难的了,主要难点在于设置状态上: 如果像平时那样设置,必定爆空间没商量: 下面是一种思路: 先把输入进来的数据按h从大到小排序,这样就可以大大减少状态数, 然后设f[i][ ...