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, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true Then 4 is the first bad version.
-----------------------------------------------------------------------------------------------------

emmm,这个题就是说的是查找第一个坏了的产品。。。。用O(n)是不行的,会超时。
C++代码:
 
 
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = ;
int right = n;
while(left < right){
int mid = left + (right - left)/;
if(isBadVersion(mid))
right = mid; //这个就是说的是当找到了查找到的是坏了的,但是并不一定是第一个坏了的,不过它的后面可以忽略,因为不是第一个坏了的。
else
left = mid + ; //这个查找的是好的,不过前面和它自己可以忽略掉,因为不是第一个坏了的。
}
return left;
}
};

官方题解:https://leetcode.com/articles/first-bad-version/

 

(二分查找 拓展) leetcode278. First Bad Version的更多相关文章

  1. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  2. (二分查找 拓展) leetcode 69. Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  3. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

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

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

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

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

  6. leetcode First Bad Version(二分查找)

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

  7. 二分查找(binary search)

    二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...

  8. 【学习记录】二分查找的C++实现,代码逐步优化

    二分查找的思想很简单,它是针对于有序数组的,相当于数组(设为int a[N])排成一颗二叉平衡树(左子节点<=父节点<=右子节点),然后从根节点(对应数组下标a[N/2])开始判断,若值& ...

  9. 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题

    如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...

随机推荐

  1. 使用 Dashboard - 每天5分钟玩转 Docker 容器技术(174)

    上一节我们完成了 Kubernetes Dashboard 的安装,本节就来实践一下. Dashboard 界面结构 Dashboard 的界面很简洁,分为三个大的区域. 顶部操作区在这里用户可以搜索 ...

  2. Spring MVC 表单校验 (七)

    完整的项目案例: springmvc.zip 目录 实例 除了依赖spring-webmvc还需要依赖jackson-databind(用于转换json数据格式)和hibernate-validato ...

  3. 【原】Java学习笔记015 - 面向对象

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 传递 值类型参数 ...

  4. RHEL6 删除软RAID

    停止使用RAID: 1.umount raid组上的硬盘的所用的分区 若使用raid组创建vg,需要删除或去激活VG 2.停止raid服务 mdadm -S /dev/md0 3.清除MBR # md ...

  5. asp.net 客户端请求到响应的整个过程

    出处:https://www.cnblogs.com/Joans/archive/2012/02/08/2342887.html 疑惑?从客户端发出一个请求,请求到达服务端如何和IIS关联起来?IIS ...

  6. Python开发【内置模块篇】configparser

    生成配置文件 import configparser config = configparser.ConfigParser() config[', 'Compression': 'yes', ', ' ...

  7. ERROR in static/js/0.5d7325513eec31f1e291.js from UglifyJs

    今天把vue项目打包是遇到这个问题.这是在服务器上打包时报的错误,本地打包不报错!很头疼!上网查了很多,发现有很多人和我遇到类似的问题,但是都没有解决我的问题!后来灵机一动,解决问题,这就跟大家说一下 ...

  8. linux下安装nodejs及npm

    转:https://www.cnblogs.com/wuyoucao/p/7011666.html 1.下载npm包 官网下载npm安装包,https://nodejs.org/en/,左边是稳定版右 ...

  9. JetBrains 全套激活 Pycharm Clion 高校学生老师免费用

    https://www.jetbrains.com/store/?fromMenu#edition=discounts https://www.jetbrains.com/zh/student/ 用高 ...

  10. Oracle 执行计划(二)------表访问的几种方式

    表访问的几种方式:(非全部) 参照表 primary key is id 1.TABLE ACCESS FULL(全表扫描):查询出该表所有数据,获取的数据执行where语句. Don’t creat ...