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 ...
随机推荐
- FineUI学习
1.无限级菜单绑定 using (DataTable dt = SqlPagerHelper.GetTableByCondition(DefaultConnection.ConnectionStrin ...
- [Training Video - 1] [Introduction to Web services]
What is a web service? http://webservicex.net/ws/default.aspx WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 跨编程 ...
- 现在有很多第三方的SDK来做直播,那么我们改选择哪一种?
现在大部分的都会借助第三方的直播技术,这样可以保证直播的质量,趣拍直播就很不错,SDK很好集成,芒果直播也在用.下面来分析一下趣拍直播的一些心得. 如何快速搭建一个完整的手机直播系统 在这个直播如火如 ...
- HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)
小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- UVa 1612 Guess (贪心+题意)
题意:有 n 位选手参加编程比赛.比赛有3道题目,每个选手的每道题目都有一个评测之前的预得分(这个分数和选手提交程序的时间相关,提交的越早,预得分越大). 接下来 是系统评测.如果某道题未通过测试,则 ...
- excel定位函数
在Excel中MATCH函数可以返回指定内容所在的位置,而INDEX又可以根据指定位置查询到位置所对应的数据,各取其优点,我们可以返回指定位置相关联的数据. MATCH函数(返回指定内容所 ...
- 从Objective-C到Swift,你必须会的(二)组合options
用过Options这个东西的人都知道,几个竖线就把这些值都和到一起了.比如: + (NSStringDrawingOptions)combine{ return NSStringDrawingTrun ...
- Objective-C 学习笔记(三) Numbers/数字
Objective-C Numbers/数字 Objective-C中提供了一系列的NSNumber和重要的工作方法: + (NSNumber *)numberWithBool:(BOOL)value ...
- java向数据库插入N条数据
为了测试mysql的索引,要向数据库先插入上万条数据,然后再测试.手动插入太麻烦,写了一段代码. 先上代码: package action; import java.sql.Connection; i ...
- XEvent--Demo--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放
--==============================================================--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放--=== ...