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

 /*************************************************************************
> File Name: LeetCode278.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Thu 19 May 2016 20:01:47 PM CST
************************************************************************/ /************************************************************************* 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.
ince 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. ************************************************************************/ #include "stdio.h" // Forward declaration of isBadVersion API.
bool isBadVersion(int version); int firstBadVersion(int n)
{
int low = ;
int high = n;
int middle; while( low <= high )
{
middle = low + ( high - low ) / ; //此处一定要用low+(high-low)/2 如果使用(low+high)/2 可能溢出
if( isBadVersion(middle) == true )
{
high = middle - ;
}
else
{
low = middle + ;
}
}
return low;
}

LeetCode 278的更多相关文章

  1. LeetCode 278. 第一个错误的版本(First Bad Version)

    278. 第一个错误的版本 LeetCode278. First Bad Version 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每 ...

  2. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

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

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

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

  4. Java实现 LeetCode 278 第一个错误的版本

    278. 第一个错误的版本 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. ...

  5. leetcode 278. First Bad Version

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

  6. (medium)LeetCode 278.First Bad Version

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

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

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

  8. Java [Leetcode 278]First Bad Version

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

  9. [leetcode]278. First Bad Version首个坏版本

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

随机推荐

  1. JDBC学习笔记(10)——调用函数&存储过程

    如何使用JDBC调用存储在数据库中的函数或存储过程: * 1.通过COnnection对象的prepareCall()方法创建一个CallableStatement *    对象的实例,在使用Con ...

  2. zznu 1914 asd的甩锅计划

    http://acm.zznu.edu.cn/problem.php?id=1914 asd的甩锅计划 时间限制: 1 Sec  内存限制: 128 MB提交: 114  解决: 10[提交][状态] ...

  3. C#Windows窗体界面设计_01_绘制三角函数_五点作图法

    binzhouweichao@163.com Visual Stutio 2010 C#开发环境 五点作图绘制三角函数,以正弦函数为例,选取一个周期[0, 2π]上的五个特殊的点,也就是横坐标0, π ...

  4. CoordinatorLayout的简单应用(材料设计新控件)

    CoordinatorLayout字面意思为:协调布局,一般作为根布局使用.关于这个布局,记录一下两个用法,备忘. 一.配合 FloatingActionBar 使用 <?xml version ...

  5. disque概要

    做项目过程接触到disque,记录一下. disque是redis之父开源的基于内存的分布式作业队列,用c语言实现的非阻塞网络服务器. disque的设计目标:Its goal is to captu ...

  6. c++未指定返回值

    int foo() { } foo返回的是随机数 函数的返回值在函数堆栈的参数后面,由一个指向寄存器的指针决定,函数返回时,调用者取指针取寄存器中的值作为返回值. 如果指定返回值,函数在返回前会将返回 ...

  7. Java中重写与重载的辨析

    摘要:在我们学习Java过程中,重写与重载使我们在面向对象里要学习的重要知识点之一,那我们看看它们有那些区别: 一.重写 1.概念:简单的说就是子类将从父类继承而来的方法重新实现一次. 2.特点: ① ...

  8. mac 杂谈

    ========== 下载 -o 文件名:-O默认文件名保存 curl -o baidu.hml http://www.baidu.com curl -O http://su.bdimg.com/st ...

  9. php+gd库的源码安装

    php+gd库的源码安装     PHP+GD安装   一.下载软件 gd-2.0.35.tar.gz          http://www.boutell.com/gd/ jpegsrc.v6b. ...

  10. 超级终端和SecureCRT进行Console口的连接

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...