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. hadoop开发MapReduce程序

    准备工作: 1.设置HADOOP_HOME,指向hadoop安装目录 2.在window下,需要把hadoop/bin那个目录替换下,在网上搜一个对应版本的 3.如果还报org.apache.hado ...

  2. Android图片加载框架之Picasso

    相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...

  3. New Moto X 2014 全版本官方解锁Bootloader图文教程

    ]秒后松开,手机就会进入fastboot模式. 如下图: <ignore_js_op> 接下来,手机连接电脑,打开刚刚的fastboot工具里面的命令提示符: <ignore_js_ ...

  4. MB11 生鲜项目原料消耗发货功能

    *&---------------------------------------------------------------------* *& 程序名: ZDQFI_925 * ...

  5. python读取/写入文件

    <Python编程:从入门到实践>读书笔记 1.读取文件并且对文件内容进行打印有三种方式: with open('test.txt') as fo: for lins in fo: pri ...

  6. AVL(二叉平衡树) 的实现

    一颗AVL树是其每个节点的左子树与右子树的高度最多差1的二叉查找树. 在插入过程中,利用旋转的办法保持这个性质. 共分四种情形: 1.  树T的左孩子的左子树上新插入节点导致破坏平衡性: 如下图左边所 ...

  7. c++ 模板 不能 分离编译

    C++Template头文件和定义分开编译的问题 (1) // Foo.htemplate<typename T>class Foo{public:void f();}; // Foo.c ...

  8. StringBuilder String string.Concat 字符串拼接速度

    首先看测试代码: public class StringSpeedTest { "; public string StringAdd(int count) { string str = st ...

  9. bootstrap select 学习使用笔记-------选中赋值及change监听丢失

    在 bootstrap 和 knockout 共同存在下使用 select 下拉选择插件,发现绑定选项.赋值之后插件不可用了,绑定的监听事件也丢失了.迫不得已在绑定选项值之后再次调用刷新,以及赋值后重 ...

  10. maven自动化构建deploy

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...