[leetcode]278. First Bad Version首个坏版本
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) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
思路
binary search
代码
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1, end = n;
while (start < end) {
// avoid overflow
int mid = start + (end-start) / 2;
if (!isBadVersion(mid)){
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
[leetcode]278. First Bad Version首个坏版本的更多相关文章
- [LeetCode] 278. First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- LeetCode OJ:First Bad Version(首个坏版本)
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- LeetCode 278.First Bad Version(E)(P)
题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- Hibernate hibernate.cfg.xml配置
数据库连接<required>: <property name="hibernate.connection.driver_class"> com.mysql ...
- hive-hbase-handler方式导入hive表数据到hbase表中
Hive与HBase的整合功能的实现是利用两者本身对外的API接口互相进行通信,相互通信主要是依靠hive-hbase-handler.jar工具类 : hive-hbase-handler.jar在 ...
- 基于RDBMS的OLAP的解决方案
BI项目如何开发: 了解OLAP的分析方法: 对数据进行多维建模分析,也就是自己设计自己的数据方体,之后程序自动生成数据方体 数据方体: 1.自动成的表结构,仅仅有你需要的列 2.生成一条SQL语句( ...
- ElasticSearch Document API
删除索引库 可以看到id为1的索引库不见了 这里要修改下配置文件 slave1,slave2也做同样的操作,在这里就不多赘述了. 这个时候记得要重启elasticseach才能生效,怎么重启这里就不多 ...
- 蓝瓶的钙,好喝的钙——windows,我要蓝屏的
原文地址:http://80x86.io/post/windows-blue-screen-0x00000050-page_fault_in_nonpaged_area 这里只截取一部分. windo ...
- bat脚本自动备份文件资源
1:xcopy命令进行文件拷贝 2:脚本内容: <span style="font-size:18px;">@echo off color 0D MODE con: ...
- 并发基础(六) 线程Thread类的start()和run()
start()和run()方法对于刚接触线程的人来说,会有点混淆,有点难理解,一般都会有以下疑问: 一.start( )方法 1.为什么需要start方法:它的作用是什么: start方法的作用就是将 ...
- HTML|CSS之HTML常用标签
知识内容: 1.标签 2.head内标签 3.body内常用标签 注:本人使用的HTML为HTML5 一.标签 1.标签格式 标签的语法: <标签名 属性1=“属性值1” 属性2=“属性值2”… ...
- python拓展2 collections模块与string模块
知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...
- Ingress.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...