题目描述:(说明中有简单翻译)

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.

Example:

Given n = 5

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true Then 4 is the first bad version. 

要完成的函数:

bool isBadVersion(int version);

int firstBadVersion(int n)

说明:

1、这道题给定一个函数isBadVersion(),每次输入一个整数,比如调用一下函数isBadVersion(4),就会知道结果是0/1,也就是4不是/是一个坏版本。

假设你是一个产品经理,需要找出版本序列中第一个坏的版本,比如[0,0,0,1,1]是你调用了上述函数去查找前五次版本评价结果,那么第4个就是第一个坏版本。

此时给你一个整数n,在[1,n]的闭区间中,判断第几个数是第一个坏版本。要求尽量少调用isBadVersion()函数。

保证在[1,n]的闭区间中存在badversion。

2、这道题很明显,就是一道二分查找的题目,查找结束的条件是,上限=下限+1,而这个时候我们返回上限就可以了。

代码如下:(附详解)

    int firstBadVersion(int n)
{
int downlim=1,uplim=n,mid;
if(isBadVersion(1))//边界条件,如果n==1 或者 n==2并且第一个数是badversion 或者 n>=3并且第一个数就是badversion
return 1;
if(n==2)//边界条件,如果n==2并且前面没有成功return,那么必然第2个是badversion
return 2;
while(uplim!=(downlim+1))//退出循环的条件
{
mid=downlim+(uplim-downlim)/2;//**后面讲解**//
if(isBadVersion(mid))
uplim=mid;//不断更新上限
else
downlim=mid;//不断更新下限
}
return uplim;
}

标**那里,我们一般情况下都是用mid=(uplim+downlim)/2,但是有些时候这样做会出错,比如uplim和downlim都很大,相加结果会溢出,这时候就不适合这样写了。

我们把它改成mid=dowmlim+(uplim-downlim)/2,同样的结果,这样就不会溢出了。

上述代码实测2ms,beats 100% of cpp submissions。

leetcode-278-First Bad Version(注意不要上溢)的更多相关文章

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

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

  2. leetcode 278. First Bad Version

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

  3. (medium)LeetCode 278.First Bad Version

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

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

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

  5. Java [Leetcode 278]First Bad Version

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

  6. [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(E)(P)

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

  8. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  9. 278. First Bad Version - LeetCode

    Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...

  10. 【leetcode】278. First Bad Version

    problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...

随机推荐

  1. Qt Customize QVariant

    Customize QVariant #include <QCoreApplication> #include <QVariant> #include <QDebug&g ...

  2. hack vba password, en useful...

    Unbelivibale, but I found a very simple way that really works! Do the follwoing: 1. Create a new sim ...

  3. sqlserver select 查询字段if判断用法

    SELECT TOP 1000 [id],      case when group_id>1 then 'vip'           else '普通会员'           end  F ...

  4. Android 上传文件到XP

    Android部分: AsyncHttpClient client = new AsyncHttpClient(); RequestParams requestParams = new Request ...

  5. [leetcode] 11. Same Tree

    因为我刷题是难度不是按发布日期,所以就有可能遇到这种情况,比如这个... Given two binary trees, write a function to check if they are e ...

  6. MSP430 G2553 Launchpad实现电容测量

    一.基本原理 对于Source-Free RC电路,其电容放电的特性可以描述为: 其中V0是电容的初始电压,t是放电时间,R是串接的电阻阻值,C是电容值,v(t)是t时刻电容上的电压.因此,若已知V0 ...

  7. Firefox mobile (android) and orientationchange

    Firefox for Android does not support the orientationchange event but you can achieve the same result ...

  8. bootstrap-table简单使用

    开发项目时总想着能不能有一款插件包含分页,查询等常用功能,后来发现了bootstrap-table 直接看代码和效果图 <!DOCTYPE html> <html lang=&quo ...

  9. 今天踩过的坑——structs和mysql

    1 在action中写了interceptor-ref就不会用defaultStack啦.得自己补上2 继承CookiesAware是不够的,得在action中配置一下 <interceptor ...

  10. css细节复习笔记——基本视觉格式化

    css包含如此开放.如此强大的一个模型,对于这样一个模型,可以有无数种方法结合应用各种属性,可以得到的效果数不胜数. 基本框 css假定每个元素都会生成一个或多个矩形框,这称为元素框.各元素中心有一个 ...