博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
First Bad Version
阅读量:7226 次
发布时间:2019-06-29

本文共 1094 字,大约阅读时间需要 3 分钟。

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 left=1;        int right=n;        while(left<=right)        {            int mid=left+((right-left)>>1);            if(isBadVersion(mid))            {                if(mid==1||mid>1&&!isBadVersion(mid-1))                    return mid;                right=mid-1;            }            else                left=mid+1;        }        return -1;    }};

 

转载地址:http://uzufm.baihongyu.com/

你可能感兴趣的文章
创建一个Struts2项目maven 方式
查看>>
数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
查看>>
纯 javascript 半自动式下滑一定高度,导航栏固定
查看>>
「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
查看>>
Rancher-k8s加速安装文档
查看>>
create-react-app项目添加less配置
查看>>
ucore操作系统实验笔记 - 重新理解中断
查看>>
leetcode46 Permutation 排列组合
查看>>
Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
查看>>
从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
查看>>
JS题目及答案整理
查看>>
C++11: atomic 头文件
查看>>
听说你叫Java(二)–Servlet请求
查看>>
vue脚手架vue-cli
查看>>
算法---两个栈实现一个队列
查看>>
redis学习笔记(三):列表、集合、有序集合
查看>>
TypeScript迭代器
查看>>
python学习笔记 - ThreadLocal
查看>>
基于组件的设计工作流与界面抽象
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>