题目:
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.

思路:
就是如果一个坏版本之后的所有版本都为坏版本,要求找出第一次出现坏版本的位置,也就是制造坏版本的那个原始坏版本。
利用二分查找,但是边界条件稍微有一点不一样,具体见代码注释。

代码:

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);class Solution {
public:int firstBadVersion(int n) {int low=1;//初始化low为1,high为nint high=n;int mid=(high-low)/2+low;//mid=(high-low)/2+low,不使用mid=(high+low)/2是为了防止high+low溢出while(low<high){//循环边界条件if(isBadVersion(mid)){//如果当前位置为坏版本high=mid;//那么high为mid(不要用mid-1),要将mid也算进去mid=(high-low)/2+low;//更新mid}else{//如果当前位置不是坏版本,也就是坏版本在之后 low=mid+1;//这里low是mid+1mid=(high-low)/2+low;//更新mid}}return mid;//最终返回的mid即为第一次出现的坏版本}
};

程序输出: 0ms

LeetCode 278. First Bad Version相关推荐

  1. [东哥的leetcode刷题日记] leetcode 278 :First Bad Version

    leetcode 278 :First Bad Version 题目链接: https://leetcode-cn.com/problems/search-insert-position/ 难度: 简 ...

  2. LeetCode算法入门- Compare Version Numbers -day14

    LeetCode算法入门- Compare Version Numbers -day14 题目描述: Compare two version numbers version1 and version2 ...

  3. LeetCode 278. 第一个错误的版本(二分查找)

    1. 题目 你是产品经理,目前正在带领一个团队开发新的产品. 不幸的是,你的产品的最新版本没有通过质量检测. 由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n ...

  4. Leetcode 278.第一个错误的版本

    Time: 20190909 Type: Easy 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错 ...

  5. Java实现 LeetCode 278 第一个错误的版本

    278. 第一个错误的版本 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. ...

  6. leetcode 278. 第一个错误的版本(Java版)

    题目 https://leetcode-cn.com/problems/first-bad-version/ 题解 二分查找即可,注意可能会溢出,过程要用 long 类型 /* The isBadVe ...

  7. leetcode 278. 第一个错误的版本(二分)

    题目 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个版本 ...

  8. 278. First Bad Version

    原题链接:https://leetcode.com/problems/first-bad-version/description/`` 实现如下: /*** Created by clearbug o ...

  9. 暑期LeetCode打卡

    文章目录 二分 LeetCode 69. Sqrt(x) LeetCode 35. Search Insert Position LeetCode 34. Find First and Last Po ...

最新文章

  1. Akka的Hello World(三)Actor失败处理
  2. 【大数据】分布式集群部署
  3. nginx自定义500、404错误页面
  4. php post请求后端拿不到值_Ajax 提交POST后,后台php 无法获取$POST值
  5. spark eventLoop模型
  6. P1702 突击考试
  7. vs 2012/2013 等工具中,使用正则表达式,查找、替换
  8. (转)ApplicationDomain
  9. 精译丨美国2017年最值得投资的7大共同基金
  10. 机械师电脑_必看!面对电脑玩游戏卡顿,需要做些什么?
  11. slickedit调试linux内核,SlickEdit使用(设置篇)
  12. 智能制造与大数据平台
  13. AQS中非公平锁的实现原理简介
  14. 基于自适应显着性的图像分割(源码开放)
  15. 深夜碎碎念,肿瘤NGS基因检测的寒冬
  16. fixed income
  17. 个人学习(解决)练习ssm框架遇到的问题No qualifying bean of type ‘service.BookTypeService‘ available:
  18. 肖邦圆舞曲14首 个人赏析
  19. 使用Unity粒子系统放烟花
  20. 服务器连局域网网卡一直不亮的解决方法

热门文章

  1. Win10+Torch1.9+CUDA11.1成功配置YOLOX预测环境
  2. 什么是端到端模型(end-to-end learning)?
  3. vue 中indexof_大厂Vue最佳实践总结,提高竞争力!
  4. 连接linux工具有哪些_推荐 10 个不错的网络监视工具,值得收藏
  5. 神经网络与深度学习 吴恩达 第一课第四周 习题
  6. 如何解决IIS配置报错问题:存储空间不足?
  7. 流量主系列|微信AI人脸转换小程序源码
  8. centos php memcache扩展,linux centos 安装php的memcache扩展
  9. mysql安装忘了root_MySQL - 安装:MySQL忘记root密码的解决办法
  10. linux系统输入法怎么安装教程视频教程,Linux怎么安装中文输入法