[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 ...
随机推荐
- [UE4]Axis Mappings轴映射和动作映射Action Mappings的区别
这里添加的映射只能在“玩家角色(Character)”的事件蓝图中使用. 从它们设置和蓝图使用界面就能看出他们的区别
- echo off
就是说关闭回显 @echo off并不是DOS程序中的,而是DOS批处理中的.当年的DOS,所有操作都用键盘命令来完成,当你每次都要输入相同的命令时,可以把这么多命令存为一个批处理,从此以后,只要运行 ...
- Spark学习笔记1:Spark概览
Spark是一个用来实现快速而通用的集群计算的平台. Spark项目包含多个紧密集成的组件.Spark的核心是一个对由很多计算任务组成的,运行在多个工作机器或者是一个计算集群上的应用进行调度,分发以及 ...
- php for 循环使用实例介绍
for 循环用于您预先知道脚本需要运行的次数的情况. 语法 for (初始值; 条件; 增量) { 要执行的代码; } 参数: 初始值:主要是初始化一个变量值,用于设置一个计数器(但可以是任何在循环的 ...
- node多进程
内容: 1.多进程与多线程 2.node中多进程相关模块的使用 1.多进程与多线程 多线程:性能高:复杂.考验程序员 多进程:性能略低:简单.对程序员要求低 Node.js中默认:单进程.单线程,但是 ...
- Coxph model Pvalue Select
I am calculating cox propotional hazards models with the coxph function from the survival package. ...
- javascript日期相减,求时间差
//计算时间差 var from_date = new Date(from_time); var end_date = new Date(end_time); var time_different = ...
- Latex 箭头、下标、符号上下写文字、正方形和三角形
0. hat $abc\hat def$ $ab\widehat{cde}f$ 1. 箭头上的文字 $\underrightarrow{\text{你的文字}}$ ($A \xlefta ...
- 手动控制IIS Express的两个常用方法
由于VS在开发WEB应用程序时,每次都需要重新启动IIS Express,速度太慢了,如果改为手动控制IIS Express启动,那么可以直接编译应用程序后,直接刷新页面,那么速度会更快. 因此需要常 ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...