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的更多相关文章

  1. First Bad Version——LeetCode

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

  2. First Bad Version leetcode java

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

  3. 278. First Bad Version - LeetCode

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

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

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

  5. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  6. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  7. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  9. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

随机推荐

  1. PowerShell批量配置VM端点

    我们可以通过PowerShell脚本批量添加VM端点.请您参考以下方案. 准备工作 – PowerShell连接China Azure 1. 从官网下载页面,下载并安装Windows Azure Po ...

  2. Redis数据结构以及Strings型操作

    Redis数据结构图: Strings型   <String key,String value>: keys * 查看所有key get 获取key的value值 append 向key对 ...

  3. datagrid的右键菜单

    1. 2.右键菜单,主要是用onRowContextMenu:function(e,index,row){}方法来实现 onRowContextMenu:function(e,index,row){ ...

  4. HttpRunnerManager 接口自动化测试平台 搭建实践

    一.需要准备的知识点 1. linux: 安装 python3.nginx 安装和配置.mysql 安装和配置 2. python: django 配置.uwsgi 配置 二.我搭建的环境 1. Ce ...

  5. Win 10激活

    Win10专业版激活(亲测有效) 来源:http://jingyan.baidu.com/article/295430f1ce2e880c7e0050ff.html 1.首先,我们先查看一下Win10 ...

  6. python2.7运行报警告:UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal解决办法

    1. 程序源代码报错部分: #选择年级if grade == '幼升小': outline.nianji().pop(0).click()elif grade == "一年级": ...

  7. mate viewport

    <meta name="viewport" content="width=device-width,height=device-height,initial-sca ...

  8. ibatis selectKey

    <insert id="insert" parameterClass="A"> <selectKey keyProperty="uu ...

  9. Java和C#中神奇的String

    Java String: String 类适用于描述字符串事物.该类是不可以被继承的.我们主要学习: 1字符串特性.字符串最大的特性:一旦被初始化就不可以被改变.重赋值只是改变了引用. 2字符串操作. ...

  10. 解决Win10 中打开VS2012 出现“ASP.NET 4.0 尚未在 Web 服务器上注册”

    系统升级为win10后,在使用vs2012打开原来的项目时,会出现“ASP.NET 4.0 尚未在 Web 服务器上注册”的问题,如图: 想到在win8.1系统下,也出现过同样的问题,就直接使用命令提 ...