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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
/**
* @param {function} isBadVersion()
* @return {function}
*/
var solution=function(isBadVersion){
/**
* @param {integer} n Total versions
* @return {integer} The first bad version
*/
return function(n){
var start=1;
var end=n;
while(start<=end){
var middle=((end-start)>>1)+start;
if(isBadVersion(middle)) end=middle-1;
else start=middle+1;
}
return end+1;
};
};
First Bad Version的更多相关文章
- ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0
ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...
- java -version 问题
我把 JAVA_HOME 从8改成了 7 , 为什么还是 显示的8啊 ! E:\sv0\jars>java -version java version "1.8.0_111" ...
- 记一次jdk升级引起的 Unsupported major.minor version 51.0
之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...
- Java–cvc-complex-type.4:Attribut ‘version’ must appear on element ‘web-app’
问题解析: 在web.xml中的以下代码中 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" ...
- 在idea中maven项目jdk编译version总是跳到1.5
bug描述 项目ide: idea 项目构建工具:maven bug现象:每次修改pom之后,idea自动扫描一遍,然后发现默认的compile级别跳到5.0. 每次手动去setting里修改comp ...
- 未能加载文件或程序集“Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5”或它的某一个依赖项。系统找不到指定的文件。
在创建ASP.NET MVC项目过程中发生了这个异常 未能加载文件或程序集"Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0 ...
- 无法解决“Microsoft.SharePoint.Security, Version=15.0.0.0,”与“Microsoft.SharePoint.Security, Version=14.0.0.0”之间的冲突
VisualStudio 2013创建控制台项目,.NetFramework选为4.5.生成目标平台:x64.然后添加对Microsoft.SharePoint.dll的引用. 生成项目时," ...
- Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...
- 未能加载文件或程序集“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件
ASP.NET 运行时错误:针对类型System.Web.Mvc.PreApplicationStartCode的应用程序邓启动初始化方法Start 引发了异常,显示下列错误消息: 未能加载文件或程序 ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
随机推荐
- MySql命令——命令行客户机的分隔符
delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...
- UVa1592 数据库(摘)
输入一个n行m列的数据库(1<=n<=10000,1<=m<=10),是否存在两个不同行r1,r2和两个不同列c1,c2,使得这两行和这两行相同(即(r1,c1)和(r2,c1 ...
- n个数的最大公约、最小公倍数
#include <cstdio> #include <cstring> using namespace std; #define N 1010 //两个数的最大公约数和最小公 ...
- 高手总结的CSS执行顺序及其优先权问题汇总
今天在看一本书时又看到了”CSS优 先权“这个问题,感觉这个问题还是比较重要的,也算是样式的特异性吧,尤其是在面对较多.较深层.较复杂的样式属性时,理解CSS的加权计算方法对于重写 样式属性之类的问题 ...
- [Head First Python]2. python of comment
1- 多行注释 ''' ''' 或 """ """ '''this is the standard way to include a mul ...
- Anniversary party(POJ 2342 树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5767 Accepted: 3335 ...
- LinqToSql增加、修改、删除---(转载)
LinqToSql ,EF:外键实体的CRD 2011-03-06 21:56:26| 分类: linq | 标签:编程 |字号 订阅 接触LinqTosql是很久的事情了, ...
- Java 学习 第二篇;面向对象 定义类的简单语法:
1:基本知识 [public / protected / private] class 类名 { 零个到多个构造器定义; 零个到多个属性; 零个到多个方法; } 其中类中各个成员之间的顺序没有关系,且 ...
- qt编程有何替代品(没见过cairo graphics)
Direct2D www.gaclib.net WPF 图形方面,c++标准有最新的提议(http://isocpp.org/files/papers/N3888.pdf),把cairo graphi ...
- C# is 与 as 运算符
as运算符有一定的适用范围,它只适用于引用类型或可以为null的类型,而无法执行其他的转换,如值类型的转换以及用户自定义的类型转换,这类转换应该适用强制转换表达式来执行.as当转换不了的时候返回nul ...