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. Delphi开发ocx插件的调试

    Delphi开发ocx苦于调试,网上看了下大概配置: IE调用ocx调试配置,在当前ocx工程  run-->parameters-->host application 里面配置IE的程序 ...

  2. 03 javadoc

    javadoc从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档 1.标签.命令格式: 2.使用方式: 2.1 dos命令行格式:javadoc XXX.java 2.2 ec ...

  3. 获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)

    1.获取电池电量(一般用百分数表示,大家自行处理就好) 1 2 3 4 -(CGFloat)getBatteryQuantity {         return [[UIDevice current ...

  4. CentOS_6.5 64位系统,安装git服务器+客户端

    ================ git服务器安装 ==================== CentOS安装Git服务器 Centos 6.4 + Git 1.8.2.2 + gitosis## . ...

  5. android中的Cursor类

    转载: 使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿 ...

  6. Lua学习笔记(二):基本语法

    Lua学习指南:http://www.lua.org/manual/ 首先我们要明确的一点是:在Lua中,除了关键字外一切都是变量. Lua关键字 可以查看这个地址:http://www.lua.or ...

  7. Epplus 使用的简单介绍

    操作Excel的主要有以下类库: MyXls(http://sourceforge.net/projects/myxls/) Koogra(http://sourceforge.net/project ...

  8. 怎么删除windows中无用的服务

    搜索cmd->以管理员身份打开 输入sc delete  服务名 回车即可

  9. HTML5 - HTML5 postMessage API 注意事项

    一:发送 window.postMessage("Hello, world", "http://127.0.0.1:8080"); 注意,必须要加上http:/ ...

  10. 算法:排序----Java选择排序

    public static void selectionSort(int[] arr) { int len = arr.length; for (int i = 0; i < len; i++) ...