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. ...
随机推荐
- Travis-CI的初步了解和测试程序的进一步编写
一. Travis-CI部分 最近基本都在研究Travis-CI的使用.CI是continue integration(持续集成)的缩写,Travis应该是给我们提供免费服务器的组织.下面介绍一下其使 ...
- php对图片反色处理
今天有个需求用php对图片进行反色,和转灰,之前不知道可不可行,后来看到了imagefilter()函数,用来转灰绰绰有余,好强大: imagefilter($im, IMG_FILTER_GRAYS ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- HDU 5294 Tricks Device 最短路+最大流
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...
- bzoj 3142 数学
找规律后可以之后答案就是 k^(m-1)*(n-(m-1)*k)+(m+(m-1)*k+1)*k^(m-1) div 2 /************************************** ...
- 设计模式之命令模式(Command)
#include <iostream> #include <string> using namespace std; class Receiver { public: void ...
- Dynamic Programming - Part1
public static void main(String[] args) { //give N, find the number of different ways to write N as t ...
- [工作积累] 32bit to 64bit: array index underflow
先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression ...
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- MVC bundle(CSS或JS)
无论是有asp还是asp.net,还是php做网站经验的都知道当我们需要css或者js文件的时候我们需要在<head></head>标签中间导入我们需要的js或者css文件的路 ...