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.

Example:

Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true Then 4 is the first bad version. 

思路

binary search

代码

 /* 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 start = 1, end = n;
while (start < end) {
// avoid overflow
int mid = start + (end-start) / 2;
if (!isBadVersion(mid)){
start = mid + 1;
} else {
end = mid;
}
}
return start;
}

[leetcode]278. First Bad Version首个坏版本的更多相关文章

  1. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  2. [LeetCode] First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  3. LeetCode OJ:First Bad Version(首个坏版本)

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. leetcode 278. First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  5. (medium)LeetCode 278.First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  6. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  7. Java [Leetcode 278]First Bad Version

    题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

  8. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  9. LeetCode OJ:Compare Version Numbers(比较版本字符串)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. win2003 server的域用户加入本地管理员组

    很久没有搞windows的域管理,今天碰到一个问题,一直找不到解决方法,一直在网络找资料,直到刚刚才找到方法,方法如下: C:\>net localgroup administrators li ...

  2. javascript节点操作replaceChild()

    replaceChild(a,b)是用来替换文档中的已有元素的 参数a:要插入的节点, 参数b:要替换的节点 var oDiv = document.getElementById("guoD ...

  3. keras基础-优化策略:mini-batch gradient decent

    参考<Keras中文文档>http://keras-cn.readthedocs.io/en/latest/ 相关概念:神经网络优化器(优化策略).梯度下降.随机梯度下降.小批的梯度下降( ...

  4. 【Linux_Unix系统编程】Chapter4 文件IO

    Chapter4 文件IO 4.1 概述 文件描述符 == Windows的句柄 标准文件描述符: 0 标准输入 STDIN_FILENO stdin 1 标准输出 STDOUT_FILENO std ...

  5. CSS3 圆角属性 border-radius和-webkit-border-radius使用

    CSS3 圆角属性 border-radius 在 CSS3 中新增了一个 border-radius 边框半径属性,即大家常用的圆角效果.这使得制作圆角将不再麻烦,只需对所用对象加一个 border ...

  6. Select算法(最坏复杂度O(n))

    #include<iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm&g ...

  7. ES6进一步整理

    内容: 1.变量及赋值 2.函数 3.数组及json 4.字符串 5.面向对象 6.Promise 7.generator 8.模块 1.变量及赋值 (1)ES5变量定义 var:     可以重复定 ...

  8. PHP提取多维数组指定一列的方法大全

    目录 1 array_column函数法 2 array_walk函数法 3 array_map函数法 4 foreach循环法 5 array_map变种 PHP中对多维数组特定列的提取,是个很常用 ...

  9. 2.1_Scikit-learn数据集

    scikit-learn数据集 我们将介绍sklearn中的数据集类,模块包括用于加载数据集的实用程序,包括加载和获取流行参考数据集的方法.它还具有一些人工数据生成器. sklearn.dataset ...

  10. svn快速安装

    windows版本控制系统. 用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用 软件下载 VisualSVN-Server:http://subversion.apa ...