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.

这个是简单的二分法,但是二分法的细节还是要注意一下。相当于从00000...11111中选出第一个1的。二分法的关键就是应该确保要找的值一定应该咋迭代的那个区间之中,比如这里的mid=1的时候,取end = mid, mid = 0的时候,start = mid + 1; 这样可以保证第一个1时钟在这个区间之中,追后可以得到第一个1.

还有个细节就是mid不适合用(start + end)/2,这样有可能导致溢出的情况。

 // Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int start = ;
int end = n;
int mid;
while(start < end){
mid = start + (end - start)/;
if(isBadVersion(mid))
end = mid;
else
start = mid + ;
}
return start;
}
};

二分法的简单变种而已,java版本代码如下所示:

 public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int beg = 1;
int end = n;
while (beg <= end) {
int mid = beg+(end-beg)/2;
if(isBadVersion(mid)){
end = mid - 1;
}else{
beg = mid + 1;
}
}
return beg; //注意边界条件,为什么返回的是beg,因为beg加上1的时候判断正好不是坏的版本,加上变成第一个,end达不到这个效果
}
}

LeetCode OJ: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. [LeetCode] 278. First Bad Version 第一个坏版本

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

  3. LeetCode OJ:Compare Version Numbers(比较版本字符串)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

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

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

  5. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  6. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  7. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  8. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  9. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. Linux学习笔记(5)磁盘分区(parted)

    Linux学习笔记(5)磁盘分区(parted) .演示: ()parted /dev/sdb :进入parted 分区命令(可以使用help来查看命令详细描述)(2)p :列出当前磁盘分区信息,可以 ...

  2. Spring Security教程 ---- 验证码功能的实现

    有三中方法可以实现验证码的功能 第一种是自定义一个filter,放在SpringSecurity过滤器之前,在用户登录的时候会先经过这个filter,然后在这个filter中实现对验证码进行验证的功能 ...

  3. C++中的默认成员函数

    一般而言,对于一个用户自定义的类类型,以下四个函数在用户没有自定义的情形下,会由编译器自动生成: 1.default constructor 2.copy constructor Someclass: ...

  4. python16_day07【Socket网络编程】

    一.简介 1.理解C/S,B/S 2.IOS七层模型(http://www.cnblogs.com/linhaifeng/articles/5937962.html) 二.什么是Socket 我们看看 ...

  5. 基于WinIO 3.0实现驱动级键盘模拟输入

    基于WinIO 3.0实现驱动级键盘模拟输入 一个业务场景需要使用驱动级的键盘模拟,折腾了2天,总结一下,为后人节省时间. 限制条件: 1.需要真实PC机,虚拟机不行 2.仅支持PS/2 键盘(指外接 ...

  6. 第五课 Makefile文件的制作(补充)

    序言: 前面的几节课讲解Makefile的一些基本知识也做了一些小例子实践了几下,那么到现在普通的练习则是没有问题.但是如果做项目文件较多又分层次等等还是会碰上好多问题的,这节课补充一些知识. 知识点 ...

  7. 第二课 GCC入门之静态库以及共享库

    序言: 前面一课讲了gcc的简单入门,包括gcc编译步骤:预处理:编译:汇编:链接.今天这节课就来讲下linux的库也欢迎大家吐糟共同学习. 原理: linux系统中分为2种库:静态库和共享库.静态库 ...

  8. 扯一扯 C#委托和事件?策略模式?接口回调?

    早前学习委托的时候,写过一点东西,今天带着新的思考和认知,再记点东西.这篇文章扯到设计模式中的策略模式,观察者模式,还有.NET的特性之一--委托.真的,请相信我,我只是在扯淡...... 场景练习 ...

  9. HDU 6356 Glad You Came 2018 Multi-University Training Contest 5 (线段树)

    题目中没有明说会爆int和longlong 的精度,但是在RNG函数中不用unsigned int 会报精度,导致队友debug了很久... 根据每次生成的l,r,v对区间更新m次,然后求 i*ai的 ...

  10. js小技巧(收集的)

    一.事件源对象 event.srcElement.tagName //IE浏览器 event.srcElement.type event.target.tagName //dom浏览器 event.t ...