Leetcode67: First Bad Version
来源:互联网 发布:淘宝店铺装修模板下载 编辑:程序博客网 时间:2024/05/24 05:59
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)/2; if(isBadVersion(mid)) { right = mid - 1; } else { left = mid + 1; } } return left; }};
二分查找法即可。
0 0
- Leetcode67: First Bad Version
- [刷题]First Bad Version
- first bad version
- First Bad Version
- [leetcode]First Bad Version
- First Bad Version
- LeetCode First Bad Version
- [leetcode] First Bad Version
- 278 First Bad Version
- First Bad Version
- [Leetcode]First Bad Version
- Leetcode: First Bad Version
- leetcode First Bad Version
- LeetCode || First Bad Version
- [leetcode]First Bad Version
- Leetcode: First Bad Version
- First Bad Version
- #leetcode#First Bad Version
- svm评价指标公式
- 5种服务器网络编程模型讲解
- 我 的 客 户 案 例
- “软件工程”
- hdu 5455 Fang Fang(2015 ACM/ICPC Asia Regional Shenyang Online)
- Leetcode67: First Bad Version
- Python中super的用法
- springsource tool suite下载地址分享
- tail -f log.txt命令的小技巧------观察日志log.txt的动态变化
- VIM配置
- servlet学习---第二天(GenericServlet、HttpServlet开发servler程序)
- java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet
- 访问wamp的PHPadmin时遇到Forbidden You don't have permission to access / on this server. You don't have per
- Java代码编写的30条建议