Description:

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.

二分法

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) { //0 0 0 0 0 1 1 1 1 1
int start=1, end=n;
int mid;
while(start + 1< end) {
mid=start + (end - start)/2;
//写成mid = (start+end) / 2,会造成整数越界,变成死循环。
//写成上边这样就能防止越界问题。
if(isBadVersion(mid)) {
end = mid;
}
else {
start = mid;
}
}
if(isBadVersion(start)) {
return start;
}
else {
return end;
} }
}

LeetCode——First Bad Version的更多相关文章

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

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

  2. [LeetCode] 165. Compare Version Numbers 比较版本数

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

  3. 【leetcode】Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...

  4. Leetcode First Bad Version

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

  5. 【leetcode】Compare Version Numbers(middle)

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

  6. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

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

  7. Java for LeetCode 165 Compare Version Numbers

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

  8. LeetCode First Bad Version (二分查找)

    题意: 有一个bool序列表示对应下标的版本是否出问题(下标从1开始),如果一个版本出了问题,那么其后面全部版本必定出问题.现在给出判断任意版本是否出问题的API,请找到第一个出问题的版本. 思路: ...

  9. leetcode:Compare Version Numbers

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

  10. Java [Leetcode 165]Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...

随机推荐

  1. java依赖的外部文件路径的获取

    在开发阶段一直使用以下方式调试没有问题: String path = KStream104.class.getResource("/").getFile().toString(); ...

  2. Lua 中pairs与ipairs区别

    local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...

  3. Hbase 学习(二)各种filter

    各种filter 今天的主题是Filter,hbase客户端查询的时候,自定义查询filter. 直接上例子吧,不多说别的了,第一个例子是RowFilter的. Scan scan = new Sca ...

  4. Entity Framework实体拆分

    一.概念 实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWebInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于 ...

  5. Wordpress搭建社交型小游戏网站10大步骤

    http://www.aliyun.com/zixun/content/2_8_196141.html ———————————————————————————————————————————————— ...

  6. java list分组 list里面分装的都是对象 按照对象的属性来分组

    http://www.iteye.com/problems/86110 —————————————————————————————————————————————————————————— List& ...

  7. TCP协议的问题

    Server端接收到Client端信息后不会返回给Client端 // TCPEchoServer.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h&quo ...

  8. JS三大经典变量命名法

    匈牙利命名法: 通过在变量名前面添加相应小写字母的符号标示作为前缀,标示出变量的作用域,类型等,前缀后面是一个或多个单词组合,单词描述了变量的用途,如i表示的是整数,s表示的是字符串.示例: var ...

  9. ffplay(2.0.1)中的音视频同步

    最近在看ffmpeg相关的一些东西,以及一些播放器相关资料和代码. 然后对于ffmpeg-2.0.1版本下的ffplay进行了大概的代码阅读,其中这里把里面的音视频同步,按个人的理解,暂时在这里作个笔 ...

  10. qlineedit控件获得焦点

    出处:http://blog.sina.com.cn/s/blog_640531380100wld9.html qlineedit控件获得焦点 lineEdit->setFocus();