Leetcode 278. First Bad Version (easy) (cpp)
2016-07-20 10:14
405 查看
Leetcode 278. First Bad Version (easy) (cpp)
Tag: Binary Search
Difficulty: Easy
Tag: Binary Search
Difficulty: Easy
/* 278. First Bad Version (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); class Solution { public: int firstBadVersion(int n) { int left = 1, right = n; while (left != right) { int mid = left / 2 + right / 2; if (isBadVersion(mid)) right = mid; else left = mid + 1; } return left; } };
相关文章推荐
- C++中union用法总结
- C++学习笔记——二分查找及其STL库函数
- 350. Intersection of Two Arrays II
- C++引用
- c++——指针在数组中的应用
- 基于c++或C的开源搜索引擎
- C语言中两个反正切函数atan与atan2的区别
- 169. Majority Element
- C++几个适合的OJ刷题网站
- C语言系统资源控制(getrlimit && setrlimit)
- 浅析C语言中的setjmp与longjmp函数
- C++ Prefix Tree 粗略实现
- C语言goto语句以及用goto语句构成循环
- C/C++ 全局变量和局部变量在内存里的区别?堆和栈
- C语言goto语句
- 详细总结C++的排序算法
- C,C++文件和目录操作的一些整理
- 高速缓存对c程序的影响
- C语言中float, double类型在内存中的存储方式
- ubuntu用C++获取ip地址和mac地址