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. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  2. Define custom @Required-style annotation in Spring

    The @Required annotation is used to make sure a particular property has been set. If you are migrate ...

  3. thymeleaf学习

    一.简单表达格式:   thymeleaf的官方参考文档 1.变量的表达式:${...} 2.选择变量表达式:*{...} 3.信息表达:#{...} 4.链接URL表达式:@{...} 二.字面值 ...

  4. Quality Center 使用IE8异常浏览器打开

    需要装2个软件 1.  Microsoft Visual C++ 2005 Redistributable Package (x64) 2.  dotnetfx35.exe 配置IE的选项 使用兼容模 ...

  5. Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)

    题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...

  6. Android访问WebService的两种方法

    首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...

  7. tomcat启动很慢的原因

    启动后tomcat显示的代码如下: 2014-4-3 10:50:15 org.apache.catalina.core.AprLifecycleListener init 信息: The APR b ...

  8. NEUOJ 1117: Ready to declare(单调队列)

    1117: Ready to declare 时间限制: 1 Sec  内存限制: 128 MB 提交: 358  解决: 41 [提交][状态][pid=1117" style=" ...

  9. UVA 11990 ``Dynamic'' Inversion 动态逆序对

    ``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...

  10. UOJ #148. 【NOIP2015】跳石头 二分

    #148. [NOIP2015]跳石头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/148 Descripti ...