Leetcode 278. First Bad Version
来源:互联网 发布:g71编程实例 编辑:程序博客网 时间:2024/04/29 11:08
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
你是一个产品经理,现在带领一个团队跟进一个新的产品。不幸的是,最新版本的产品质量检测失败了。每一个版本都是在上一个版本的基础上发展而来的 ,在一个质量不过关的版本以后的所有版本质量都是不过关的。
假设你有n个版本[1,2,…,n],同时 你想找出第一个坏的版本,导致接下来的版本都坏了。
给你一个api bool isBadVersion(version) 返回版本是否是坏的。 实现一个函数找出第一个坏的版本,你应该最小化调用api的次数。
解法:利用二分法判断即可找到第一个坏的版本。
public class Solution extends VersionControl { public int firstBadVersion(int n) { int min = 1, max = n, mid = 0; while(min <= max){ mid = min + (max - min) / 2; if(isBadVersion(mid)){ max = mid - 1; } else { min = mid + 1; } } return min; }}
- 278. First Bad Version LeetCode
- LeetCode 278. First Bad Version
- 【LeetCode】278. First Bad Version
- Leetcode 278. First Bad Version
- leetcode 278. First Bad Version
- LeetCode 278. First Bad Version
- LeetCode *** 278. First Bad Version
- [leetcode] 278. First Bad Version
- LeetCode 278. First Bad Version
- leetcode-278. First Bad Version
- LeetCode 278. First Bad Version
- [Leetcode] 278. First Bad Version
- Leetcode 278. First Bad Version
- [leetcode]278. First Bad Version
- LeetCode 278. First Bad Version
- Leetcode 278. First Bad Version
- LeetCode-278.First Bad Version
- LeetCode - 278. First Bad Version
- 跟我一起学习VIM - The Life Changing Editor
- jar文件转换成Maven目录
- Java io流
- 无向图的边双联通分量,点双联通分量
- 徒步游记
- Leetcode 278. First Bad Version
- 如何实现推送引导
- 整数奇偶排序
- QT5问题汇总
- dos窗口直接进入指定目录
- Qt在pro中管理多个子项目(子工程)
- could not execute query 项目中使用使用Hibernate作为持久层框架时,数据库改名后容易忽略的问题
- Maven远程部署Tomcat
- 【转】HashMap源码解析