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. python之enumerate枚举 第二篇(六):enumerate枚举

    [Python之旅]第二篇(六):enumerate枚举   python enumerate枚举 摘要: 1.普通情况下打印列表中索引号及其对应元素     使用下面的循环: 1 2 3 4 5 6 ...

  2. 10.25 noip模拟试题

    今天题目略水2333 依旧不粘题目了23333 T1 /*数学题 给定n个斜率 求有多少个三元组 保证两两斜率不同 ans=C(n,3)-ΣC(len[i],2)*(n-len[i])-ΣC(len[ ...

  3. tj

    --统计set @collSql='select sum(case Ca_IssueType when 0 then 1 else 0 end) as IssueCount,sum(case when ...

  4. 我的网站终于连续一星期,ip数1000以上了

    相信每个程序员都有一个站长梦,我也是如此.说来惭愧,从2015年4月份开始,我投入了50块的域名费用,50块的空间费用,现在才回本. 由于终于从百度联盟的手里赚了一百块,按耐不住的菜鸟之冲动,于是决定 ...

  5. Android开发常见错误类型一览表

    这是我的第一个博客,我会一直添加我在Android开发中遇到的错误,用来记录我开发中踩过的那些坑 ------------------------分割线------------------------ ...

  6. Android中dip,dp,sp,pt和px的区别

    dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...

  7. LINQ to SQL 运行时动态构建查询条件

    在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...

  8. C# 多线程详解

    1.使用多线程的几种方式 (1)不需要传递参数,也不需要返回参数 ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值. 复制代码 代码如下: cl ...

  9. ios开发之UIImageView

    废话少说,直接进入正题!!! 1.创建一个UIImageView: 创建一个UIImageView对象的几种方法: UIImageView *imageView1 = [[UIImageView al ...

  10. javascript DOM艺术

    一.DOM基础1.节点(node)层次Document--最顶层的节点,所有的其他节点都是附属于它的.DocumentType--DTD引用(使用<!DOCTYPE>语法)的对象表现形式, ...