278. First Bad Version
2016-01-25 14:12
113 查看
278. First Bad Version
Difficulty: Easy
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.
Difficulty: Easy
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); int firstBadVersion(int n) { int med; int n1=1,n2=n; while(n1<n2) { med=(n2-n1)/2+n1; //若 "med=(n1+n2)/2" 会出现超时错误,因为直接相加可能溢出; if(isBadVersion(med)) { n2=med; } else { n1=med+1; } } return n1; }
相关文章推荐
- windows防火墙命令详解(程序启用命令行执行即可)
- Fiddler抓取手机APP数据包
- Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建
- 用PL/SQL Developer直接COPY SQL语句变成StringBuffer语句[转]
- Handler,Looper消息传递机制
- python多线程与多进程 超简单使用
- 第五篇 UICollectionView
- 汇编题目:在DOS下,按F1键后改变当前屏幕的显示颜色
- 什么是词干化处理
- Java的数据类型
- swfupload
- 【Echarts】数据可视化_前端页面实现尝试
- perl 实现微信登录 获取好友 群信息的简单实现
- 支付宝PM告诉你,互联网产品经理的工作职责有哪些?
- Java开发中的23种设计模式详解
- 《探索式软件测试》中提到的几种需要测试的情况
- 基于BootStrap框架构建快速响应的GPS部标监控平台
- shell脚本案例
- 入门句法分析
- 数据库连接driverClass和jdbcUrl大全