博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]278. First Bad Version首个坏版本
阅读量:5235 次
发布时间:2019-06-14

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

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.

Example:

Given n = 5, and version = 4 is the first bad version.call isBadVersion(3) -> falsecall isBadVersion(5) -> truecall isBadVersion(4) -> trueThen 4 is the first bad version. 

 

思路

binary search 

 

代码

1 /* The isBadVersion API is defined in the parent class VersionControl. 2       boolean isBadVersion(int version); */ 3  4 public class Solution extends VersionControl { 5    public int firstBadVersion(int n) { 6         int start = 1, end = n; 7         while (start < end) { 8             // avoid overflow 9             int mid = start + (end-start) / 2;10             if (!isBadVersion(mid)){11                 start = mid + 1;12             } else {13                 end = mid; 14             }           15         }        16         return start;17     }

 

转载于:https://www.cnblogs.com/liuliu5151/p/9814311.html

你可能感兴趣的文章
IE11兼容IE8的设置
查看>>
windows server 2008 R2 怎么集成USB3.0驱动
查看>>
Foxmail:导入联系人
查看>>
vue:axios二次封装,接口统一存放
查看>>
vue中router与route的区别
查看>>
js 时间对象方法
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>