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:

isBadVersion(3) -> false
isBadVersion(5) -> true
isBadVersion(4) -> true

Here we are 100% sure that the 4th version is the first bad version.

分析:

代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。

你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错。

给出 n=5

调用isBadVersion(3),得到false

调用isBadVersion(5),得到true

调用isBadVersion(4),得到true

此时我们可以断定4是第一个错误的版本号

/**
* public class SVNRepo {
* public static boolean isBadVersion(int k);
* }
* you can use SVNRepo.isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
class Solution {
/**
* @param n: An integers.
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
int start = 0, end = n;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (SVNRepo.isBadVersion(mid)) {
end = mid;
} else {
start = mid;
}
} if (SVNRepo.isBadVersion(start)) {
return start;
}
return end;
}
}

leetcode 278. First Bad Version的更多相关文章

  1. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  2. (medium)LeetCode 278.First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  3. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  4. Java [Leetcode 278]First Bad Version

    题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

  5. [leetcode]278. First Bad Version首个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  6. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  7. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  8. 【leetcode】278. First Bad Version

    problem 278. First Bad Version solution1:遍历: // Forward declaration of isBadVersion API. bool isBadV ...

  9. 278. First Bad Version - LeetCode

    Question 278. First Bad Version Solution 题目大意:产品有5个版本1,2,3,4,5其中下一个版本依赖上一个版本,即版本4是坏的,5也就是坏的,现在要求哪个版本 ...

随机推荐

  1. BZOJ3226: [Sdoi2008]校门外的区间

    感觉很有趣的题呢. 每个点拆成两个,线段树维护. 不过这题难点其实在输入输出. #include<bits/stdc++.h> #define N (1<<17) #defin ...

  2. WinForm------RepositoryItemCheckEdit属性介绍

    //去掉第三种状态 editcheck1.OptionView.NullStyle = UnChecked

  3. php返回json数据中文显示的问题

    PHP5.4版本,已经给Json新增了一个选项: JSON_UNESCAPED_UNICODE.加上这个选项后,就不会自动把中文编码了. echo json_encode("厦门" ...

  4. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  5. git 推送

    echo "# shops" >> README.md git init git add README.md git commit -m "first com ...

  6. 9月5日网页基础知识 通用标签、属性(body属性、路径、格式控制) 通用标签(有序列表、无序列表、常用标签)(补)

    网页基础知识 一.HTML语言 HTML语言翻译汉语为超文本标记语言. 二.网页的分类 1.静态页面:在静态页面中修改网页内容实际上就是修改网页原代码,不能从后台操作,数据来只能来源于原于代码.静态网 ...

  7. 关于软件工程结对编程作业 PairProject : Elevator Scheduler(电梯调度算法的实现与测试)的总结

    1)结对编程队友 1106xxxx 张扬 1106xxxx 杨军 其中,此项目的编程实现主要由前者完成. 2)关于结对编程 结对编程的优点: 最直接的一点:在结对编程中,由于有另一个人在你身边和你配合 ...

  8. HTML5 常用的结构化标签整理

    一.语义化结构化标签 结构化标签优点: 1.方便浏览器处理和识别,提升了网页的质量和语义. 2.减少了大量无意义的div标签,增强代码的可读性. 结构化标签:(header,nav,body,arti ...

  9. ILMerge

    ILMerge http://www.microsoft.com/en-hk/download/details.aspx?id=17630 ILMerge 下载地址:http://www.micros ...

  10. python- shutil 高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 拷贝文件 shutil.copyfile(src, ...