LeetCode 278
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的更多相关文章
- LeetCode 278. 第一个错误的版本(First Bad Version)
278. 第一个错误的版本 LeetCode278. First Bad Version 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每 ...
- [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 ...
- [LeetCode] 278. First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Java实现 LeetCode 278 第一个错误的版本
278. 第一个错误的版本 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- [leetcode]278. First Bad Version首个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
随机推荐
- 执行原始的 SQL 查询
The Entity Framework Code First API includes methods that enable you to pass SQL commands directly t ...
- bitmap的实现方法
bitmap是一个十分有用的结构.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 适用 ...
- HDU 5781 ATM Mechine (概率DP)
ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...
- Hibernate监听器
Hibernate的事件监听机制 Hibernate中的事件监听机制可以对Session对象的动作进行监听,一旦发生了特殊的事件,Hibernate就会执行监听器中的事件处理方法 在某些功能的设计中, ...
- linux下的调试工具ltrace与strace
ltrace能够跟踪进程的库函数调用,它会显现出哪个库函数被调用,而strace则是跟踪程序的每个系统调用. 下面是一个ltrace与strace的对比 1)系统调用的输出对比 我们用输出he ...
- ASP.NET中身份验证的三种方法
Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活.Forms 验证方式对基于用户的验证授权 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- TypeScript学习笔记(四):函数
这篇笔记我们来看看TypeScript中的函数. 函数类型 在JavaScript中存在两种定义函数的方法,如下: //命名函数 function add(x, y) { return x+y; } ...
- Row_Number()over(order by....) as
出自:http://www.2cto.com/database/201307/227103.html Sql Server Row_Number()学习 Row_Number(): row_n ...
- C# winform 最小化到电脑右下角
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...