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. SVN学习一

    svn是subversion的缩写形式. svn是一个版本控制软件. 目标: 1.允许开发同时进行工作. 2.不覆盖其他人的文件变化. 3.保持历史的每一个版本. 版本控制系统分为两类: 1.集中式版 ...

  2. Part1.2 、RabbitMQ -- Publish/Subscribe 【发布和订阅】

    python 目录 (一).交换 (Exchanges) -- 1.1 武sir 经典 Exchanges 案例展示. (二).临时队列( Temporary queues ) (三).绑定(Bind ...

  3. Linux基础——硬盘分区、格式化及文件系统的管理

    1. 硬件设备与文件名的对应关系 掌握在Linux系统中,每个设备都被当初一个文件来对待. 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a-d] SCSI硬盘 /dev/sd[a-p ...

  4. T25健身视频全集+课表

    http://jianfei.39.net/thread-3639251-1.html T25健身视频全集+课表 强度适中 不伤膝盖! [复制链接]     zytttt         主题 好友 ...

  5. dogo 官方翻译 Ajax with dojo/request

    require(["dojo/request"], function(request){ request("helloworld.txt").then( fun ...

  6. 用OpenCV实现Photoshop算法(三): 曲线调整

    http://blog.csdn.net/c80486/article/details/52499919 系列文章: 用OpenCV实现Photoshop算法(一): 图像旋转 用OpenCV实现Ph ...

  7. JSONP跨站访问

    js中几种实用的跨域方法原理详解 这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协 ...

  8. Java应对Flash XSS攻击

    问题引出: 今天公司派出安全任务,说是要解决一个Flash XSS攻击,一看顿时傻眼,都没听说过.而且flash已经淘汰了,根本没研究过flash,搜了资料才开始慢慢开始工作. 要求: 1.过滤URL ...

  9. OpenGL核心技术之Gamma校正

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你/2.2次幂.Gamma校正后的暗红色就会成为(0.5,0.0 ...

  10. 判断一个对象是否有new

    C++语言中,对象没有空和不空的概念,只有对象指针才有空和不空的概念 判断对象指针是否为空只需要和NULL常量进行比较即可 如果相等,则为空,否则不为空 另外对象虽然没有空和不空的概念,但是有有效和无 ...