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. 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(注意不要上溢)的更多相关文章
- [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
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 ...
- LeetCode 278.First Bad Version(E)(P)
题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...
- 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导致的溢 ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- 【leetcode】278. First Bad Version
problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...
随机推荐
- Oracle ADF VO排序及VO的查询模式
常规应用中,当需要使用Table向终端用户展示数据时,Table中数据的显示排序一致性极大程度的影响到了客户体验.通常希望诸如多次查询结果显示顺序相同.插入数据在原数据上方等的实现. ADF为开发人员 ...
- ROS 消息发布器和订阅器Publisher, Subscriber
博客参考:https://www.2cto.com/kf/201705/639776.html 1.编写发布器节点节点(Node) 是指 ROS 网络中可执行文件.接下来,将会创建一个发布器节点(“t ...
- Java程序设计9——泛型
泛型是对集合的补充,JDK1.5增加泛型支持很大程度上都是为了让集合能记住其元素的数据类型.在没有泛型之前,一旦把一个对象丢进Java集合中,集合就会忘记对象的类型,把所有的对象都当成Object类型 ...
- Redis Quick Start [熟练版]
一.下载解压 wget http://download.redis.io/redis-stable.tar.gztar xvzf redis-stable.tar.gzcd redis-stable ...
- 再议GCC编译时的静态库依赖次顺问题
假设有如三个源代码文件: $ cat a.cpp void a() { } $ cat b.cpp extern void a(); void b() { a(); // 调用a.cpp中的a() } ...
- MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化
第9章 MySQL数据库Schema设计的性能优化 前言: 很多人都认为性能是在通过编写代码(程序代码或者是数据库代码)的过程中优化出来的,其实这是一个非常大的误区.真正影响性能最大的部分是在设计中就 ...
- [转]程序集之GAC---Global Assembly Cache
本文转自:http://www.cnblogs.com/jhxk/articles/2564295.html 1.什么是GAC?GAC解决什么问题? GAC全称为: Global Assembly C ...
- OpenSSL命令---crl2pkcs7
用途: 本命令根据CRL或证书来生成pkcs#7消息. 用法: openssl crl2pkcs7 [-inform PEM|DER ] [-outform PEM|DER ] [-in filena ...
- 数据集和JSON相互转换
使用DELPHI原生类实现数据集和JSON相互转换 JSON二要素:数组和对象.对象可以包含数组,数组可以包含对象.无层数限制.OLEVARIANT也类似,OLEVARIANT的一个元素又可以是OL ...
- ASP.NET:邮件服务器与客户端
目录: 一.概述 二.MX设置 三.使用系统的SMTP功能发邮件 四.使用hMailServer收发邮件 五.Web邮件客户端 一.概述 首先必须清楚SMTP才是提供邮件服务器的核心,收发邮件全靠SM ...