First Bad Version - LeetCode
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.
思路:二分查找。
要注意的一点是,在计算中间点 mid的值时,如果用mid = (left + right) >> 1有可能结果会溢出。
这里用 mid = (left >> 1) + (right >> 1)
式子中后面如果不加括号有可能会出错,因为会把right的值当成前面left的位移操作的距离。
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
if (n < ) return n;
int left = , right = n;
while ( left < right)
{
int mid = (left >> ) + (right >> );
bool bad = isBadVersion(mid);
if (bad)
{
if (mid == || !isBadVersion(mid - ))
return mid;
right = mid;
}
else
{
if (isBadVersion(mid + ))
return mid + ;
left = mid + ;
}
}
return ;
}
};
First Bad Version - LeetCode的更多相关文章
- First Bad Version——LeetCode
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- First Bad Version leetcode java
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 278. First Bad Version - LeetCode
Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- LeetCode算法题-First Bad Version(Java实现-三种解法)
这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
随机推荐
- Android 服务入门
前言:硬着头皮把数据库SQLite看完了,接下来就是android服务了,因为自己本身就是菜鸟,所以呢,也只是做做笔记,技术上的东西就别指望我了. 1.什么是服务呢?举个例子,百度地图,美团外卖,OF ...
- Leetcode 630.课程表III
课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...
- re.search 与 re.match的区别
search ⇒ find something anywhere in the string and return a match object. match ⇒ find something at ...
- ms sqlserver数据库主文件特别大怎么办
因为项目中需要复制数据库,作为外网测试的数据库,但是数据库特别大,复制特别费劲,即使只复制主文件,主文件也特别大. 然后百度了下,发现数据库有个收缩功能,数据库右键——任务——收缩,可以对数据库进行收 ...
- POJ 2184:Cow Exhibition(01背包变形)
题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们 ...
- FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令
FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令 来源 https://www.liurongxing.com/freebsd-tips.html 来源 http://blog.51c ...
- spring项目启动报错BeanFactory not initialized or already closed
spring项目启动的时候报如下错误: java.lang.IllegalStateException: BeanFactory not initialized or already closed - ...
- idea工具开发注意事项
pom.xml中不需要有包 <dependency> <groupId>javax</groupId> <artifactId>javaee-api&l ...
- IPFS
http://www.r9it.com/20190412/ipfs-private-net.html IPFS指令集中文版(一) https://www.jianshu.com/p/ce74b32d2 ...
- ssh(安全协议外壳)
以下来源于百度百科 https://baike.baidu.com/item/ssh/10407?fr=aladdin SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Netw ...