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. css加载会造成阻塞吗?

    终于考试完了,今天突然想起来前阵子找实习的时候,今日头条面试官问我,js执行会阻塞DOM树的解析和渲染,那么css加载会阻塞DOM树的解析和渲染吗?所以,接下来我就来对css加载对DOM树的解析和渲染 ...

  2. org.apache.commons.beanutils.BeanMap简单使用例子

    一.org.apache.commons.beanutils.BeanMap; 将一个java bean允许通过map的api进行调用, 几个支持的操作接口: Object get(Object ke ...

  3. spring整合axis2(最小配置化)的示例

    参考文档: http://blog.csdn.net/xinhaoluan/article/details/3605234 环境配置: spring-framework-3.2.7 axis2-1.6 ...

  4. Hadoop生态系统图解

    Hadoop生态架构图 参考文章: Hadoop生态系统介绍 HDFS架构 1.NaneDode:主节点,**存储文件的元数据**如文件名,文件目录结构,文件属性(生成时间,副本数量,文件权限),以及 ...

  5. Chrome浏览器扩展开发系列之六:options 页面

    options 页面用以定制Chrome浏览器扩展程序的运行参数. 通过Chrome 浏览器的“工具 ->更多工具->扩展程序”,打开chrome://extensions页面,可以看到有 ...

  6. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  7. 集群之mysql主从配置(windows和linux版)

    起因 由于网站进一步开发运行的需求,要求主机7*24小时运行正常,同时要求能够防止数据库灾难.考虑到后期的开发程度和业务量,准备向高可用系统进行改变,同时通过负载均衡提高网络性能.于是第一步就考虑到了 ...

  8. Ambari里如何删除某指定的服务(图文详解)

    不多说,直接干货! Ambari 借鉴了很多成熟分布式软件的 API 设计.Rest API 就是一个很好地体现.通过 Ambari 的 Rest API,可以在脚本中通过 curl 维护整个集群.并 ...

  9. java webservice简单的例子

    开发环境:eclipse .jdk 创建服务端 demo-webservice 创建类 HelloService.java package com.hundsun.ws.service; import ...

  10. Verilog HDL的程序结构及其描述

    这篇博文是写给要入门Verilog HDL及其初学者的,也算是我对Verilog  HDL学习的一个总结,主要是Verilog HDL的程序结构及其描述,如果有错,欢迎评论指出. 一.Verilog ...