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.

这道题比较简单,可是我却也琢磨了很久,其实看到这道题第一个反应就是二分查找。专业点说,这是一个寻找左边界的问题。

思路就是:如果头部是bad version的话,可以返回;如果mid是坏的话,坏的源头必然在head和mid之间,那么尾部就可以变为mid了。如果mid不是坏的,那么坏的必然在mid和尾部之间,所以头部会变成mid。(就是二分查找了!)

但是刚开始mid=(head+tail)/2会time exceed(因为直接加head+tail有可能会溢出)。说明【自己没有良好的代码习惯】,以前看书的时候就说,二分查找,最好写成mid=low+(high-low)/2。因为time exceed,又考虑了别的思路,导致这道题做了很久。

// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
if(n==) return -;
int head=,tail=n,mid=;
while(head<=tail){
mid=head+(tail-head)/;
if(isBadVersion(head)) return head;
if(isBadVersion(mid)) {head++; tail=mid;}
else head=mid+;
}
return -; }
};

leetcode First Bad Version(二分查找)的更多相关文章

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

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

  2. LeetCode刷题总结-二分查找和贪心法篇

    本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...

  3. LeetCode 704. Binary Search (二分查找)

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  4. leetcode刷题-- 3.二分查找

    二分查找 正常实现 题解 public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l ...

  5. Leetcode 69 Sqrt(x) 二分查找(二分答案)

    可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...

  6. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  7. cf1216E2 Numerical Sequence (hard version) 二分查找、思维题

    题目描述 The only difference between the easy and the hard versions is the maximum value of k. You are g ...

  8. LeetCode First Bad Version (二分查找)

    题意: 有一个bool序列表示对应下标的版本是否出问题(下标从1开始),如果一个版本出了问题,那么其后面全部版本必定出问题.现在给出判断任意版本是否出问题的API,请找到第一个出问题的版本. 思路: ...

  9. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

随机推荐

  1. javascript之原型(prototype)

    今天是第一次写博客,写点在javascript中重要的一个概念----原型(prototype): 原型,顾名思义,就是一切事物的模板. 柏拉图在<理想国>卷10中说:"床不是有 ...

  2. zookeeper初试

    实验环境: os-platform: windows7 x64 jdk: 1.7 参考文档: http://www.ibm.com/developerworks/cn/opensource/os-cn ...

  3. 浅析CQRS的应用部署

    CQRS,中文翻译命令和查询职责分离,它是一种架构,不仅可以从数据库层面实现读写分离,在代码层面上也是推荐读写分离的.在接口上可以更为简单 命令端定义 ICommandResult Execute(I ...

  4. 使用Web页面配置ESP8266的参数

    前言 使用Web页面配置ESP8266的参数相对于使用串口AT指令配置更加直观和简单.与配置路由器方式类似. 基本思路 基本思路是ESP8266工作AP模式下,作为TCP Server监听TCP Cl ...

  5. Mysql连接出错问题

    1.java 提示:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 处理:导入mysql-connector-java-5.1.7-bi ...

  6. Ambari安装之部署 (Metrics Collector和 Metrics Monitor) Install Pending ...问题

    问题的由来 我这里,是因为,拿这个Ambari Metrics服务在做试验!所以先删除它,再添加它,出现了安装被挂起的问题.... Am bari里如何删除某指定的服务(图文详解) 问题详细描述如下: ...

  7. FreeRTOS——队列管理

    1. 队列主要用于任务与任务.中断与任务之间的消息传递. 2. 创建队列时,请注意队列中数据单元的长度. 3. 通常情况,队列被作为FIFO(先进先出)使用,即数据从队列尾写入,从队列首读.当然,数据 ...

  8. windows环境下,anoconnda安装tensorflow

    最近对深度学习研究比较多,目前最火的Python深度学习库应该是tensorflow了. 为了方便,本人在windows下用anaconda来使用python,且同时安装了,anaconda2,3,3 ...

  9. 教你做炫酷的碎片式图片切换 (canvas)

    前言 老规矩,先上 DEMO 和 源码.图片区域是可以点击的,动画会从点击的位置开始发生. 本来这个效果是我3年前做的,只是当是是用无数个 div 标签完成的,性能比较成问题,在移动端完全跑不动.最近 ...

  10. AC自动机总结及板子(不带指针)

    蒟蒻最近想学个AC自动机简直被网上的板子搞疯了,随便点开一个都是带指针的,然而平时用到指针的时候并不多,看到这些代码也完全是看不懂的状态.只好在大概理解后自己脑补(yy)了一下AC自动机的代码,居然还 ...