278. First Bad Version

Easy

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.
package leetcode.easy;
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class FirstBadVersion extends VersionControl {
public int firstBadVersion1(int n) {
for (int i = 1; i < n; i++) {
if (isBadVersion(i)) {
return i;
}
}
return n;
} public int firstBadVersion2(int n) {
int left = 1;
int right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
} @org.junit.Test
public void test() {
FirstBadVersion firstBadVersion = new FirstBadVersion();
System.out.println(firstBadVersion.firstBadVersion1(5));
System.out.println(firstBadVersion.firstBadVersion2(5));
}
} class VersionControl {
boolean isBadVersion(int version) {
if (version >= 4) {
return true;
} else {
return false;
}
}
}

LeetCode_278. First Bad Version的更多相关文章

  1. 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 ...

  2. java -version 问题

    我把 JAVA_HOME 从8改成了 7 , 为什么还是 显示的8啊 ! E:\sv0\jars>java -version java version "1.8.0_111" ...

  3. 记一次jdk升级引起的 Unsupported major.minor version 51.0

    之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...

  4. 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=" ...

  5. 在idea中maven项目jdk编译version总是跳到1.5

    bug描述 项目ide: idea 项目构建工具:maven bug现象:每次修改pom之后,idea自动扫描一遍,然后发现默认的compile级别跳到5.0. 每次手动去setting里修改comp ...

  6. 未能加载文件或程序集“Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5”或它的某一个依赖项。系统找不到指定的文件。

    在创建ASP.NET MVC项目过程中发生了这个异常 未能加载文件或程序集"Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0 ...

  7. 无法解决“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的引用. 生成项目时," ...

  8. Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6

    未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...

  9. 未能加载文件或程序集“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件

    ASP.NET 运行时错误:针对类型System.Web.Mvc.PreApplicationStartCode的应用程序邓启动初始化方法Start 引发了异常,显示下列错误消息: 未能加载文件或程序 ...

随机推荐

  1. lambda 函数的用法

    lambda函数又叫匿名函数, 匿名函数就是没有名字的函数,不使用def语句声明的函数.如果要声名,则需要使用lambda关键字进行声明. 一般用来定义简单的函数. 1.声明一个简单的加法匿名函数: ...

  2. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  3. 如何识别和解决SQL Server中的热闩锁(PAGELATCH_EX)

    描述 在SQL Server中,内部闩锁体系结构可在SQL操作期间保护内存.通过页面上的读写操作,可以确保内存结构的一致性.从根本上讲,它具有两个类:缓冲区锁存器和非缓冲区锁存器,它们在SQL Eng ...

  4. MySQL 索引原理以及慢查询优化

    本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BTree ...

  5. Problem 8 dp

    $des$ $sol$ 记 $f_i$ 表示考虑前 $i$ 个建筑, 并且第 $i$ 个建筑的高度不变的答案, 每次转移时枚举上一个不变的建筑编号, 中间的一段一定变成相同的高度, 并且高度小于等于两 ...

  6. 【概率论】5-10:二维正态分布(The Bivariate Normal Distributions)

    title: [概率论]5-10:二维正态分布(The Bivariate Normal Distributions) categories: - Mathematic - Probability k ...

  7. AtCoder Grand Contest 011题解

    传送门 \(A\) 直接按时间排序之后贪心就可以了 const int N=1e5+5; int a[N],q[N],c,k,h,t,n,res; inline int min(R int x,R i ...

  8. 在Xilinx ISE中生成ROM时需要注意的事

    在Xilinx ISE中生成ROM时,需要指定.coe文件.需要做到两件事.其一,要使用memory_initialization_radix= ; memory_initializatoin_vec ...

  9. Large Kernel Matters —— Improve Semantic Segmentation by Global Convolutional Network(GCN全局卷积网络)

    作者认为语义分割的两个挑战是分类和定位,而这两个挑战又是比较对立的.对于分类问题,模型需要有变形和旋转不变形,而对于定位问题,模型有需要对变形敏感. 提出的GCN遵循两个主要原则: 1.对定位问题,模 ...

  10. Android中活动的最佳实践(如何很快的看懂别人的代码activity)

    这种方法主要在你拿到别人的代码时候很多activity一时半会儿看不懂,用了这个方法以后就可以边实践操作就能够知道具体哪个activity是干什么用的 1.新建一个BaseActivity的类,让他继 ...