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.

题目大意:n个版本,如果某个版本是坏的,后面都是坏的,找出第一个坏的。

解题思路:二分查找。

public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (isBadVersion(1)){
return 1;
}
int lo = 0;
int hi = n - 1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (!isBadVersion(mid + 1) && isBadVersion(mid + 2)) {
return mid + 2;
}
if (mid > 0 && !isBadVersion(mid) && isBadVersion(mid + 1)) {
return mid + 1;
} else if (isBadVersion(mid + 1)) {
hi = mid - 1;
} else if (!isBadVersion(mid + 1)) {
lo = mid + 1;
}
}
return -1;
}
}

First Bad Version——LeetCode的更多相关文章

  1. First Bad Version leetcode java

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

  2. First Bad Version - LeetCode

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

  3. 278. First Bad Version - LeetCode

    Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...

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

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

  5. [LeetCode] Compare Version Numbers 版本比较

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

  6. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  7. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  9. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

随机推荐

  1. Conversion Between DataTable and List in C#

    1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...

  2. js兼容各个浏览器的复制功能

    看似简单的复制功能,用js做起来竟然遇到各种情况.刚开始在网上搜索到复制功能的几种实现方法,但是都不兼容.最后还是用的插件代码如下 html模板 <tr> <td>1</ ...

  3. HTML5 文件域+FileReader 读取文件(二)

    一.读取文本文件内容,指定字符编码 <div class="container"> <!--文本文件验证--> <input type="f ...

  4. VsSharp:一个VS扩展开发框架(上)

    上篇:设计 一.引子 自2008年起开发SSMS插件SqlSharp(er)的过程中,有一天发现多数代码都大同小异,就像这样. Commands2 commands = (Commands2)_app ...

  5. Linq 调试

    void Main() { var MyMonitor = new Devart.Data.Oracle.OracleMonitor(); MyMonitor.IsActive = true; var ...

  6. Oracle 左连接、右连接、全外连接、(+)号作用、inner join(等值连接) (转载)

    Oracle  外连接 (1)左外连接 (左边的表不加限制)       (2)右外连接(右边的表不加限制)       (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...

  7. hdoj 4310 贪心

    不知为毛,过不了 我的代码: #include<stdio.h> int main(){ int n,a[30],b[30],temp,i,j,s1,s2; double c[30]; w ...

  8. 初探grunt.js

    package.js { "name": "ttd_v3", "version": "0.1.0", "aut ...

  9. Sass学习

    1.1下载地址: http://rubyinstaller.org/downloads 2.1 安装 SASS是Ruby语言写的,但是两者的语法没有关系.不懂Ruby,照样使用.只是必须先安装Ruby ...

  10. ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)

    网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法. 1 简单方法 //下面代码输出的 ...