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, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true Then 4 is the first bad version. 

思路

binary search

代码

 /* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1, end = n;
while (start < end) {
// avoid overflow
int mid = start + (end-start) / 2;
if (!isBadVersion(mid)){
start = mid + 1;
} else {
end = mid;
}
}
return start;
}

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

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

  3. LeetCode OJ:First Bad Version(首个坏版本)

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

  4. leetcode 278. First Bad Version

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

  5. (medium)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 二分查找(二分下标)

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

  7. Java [Leetcode 278]First Bad Version

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

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

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

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

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

随机推荐

  1. phpexcel导入数据出现PHPExcel_RichText Object解决办法

    在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...

  2. JNDI的学习与使用

    JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  3. [ffmpeg]deocde audio(v3.3.2)

    /* * Copyright (c) 2001 Fabrice Bellard * * Permission is hereby granted, free of charge, to any per ...

  4. NOIP考前复习-数制转换,数论模板与文件读写

    数制转换有两种题型,一般一题,分值1.5分. 题型一:R进制转十进制 解法就是:按权展开,但要注意各个位的权,最低位(最右边)的权是0次方,权值为1. 纯整数的情况: (11010110)2 = 1× ...

  5. 常用数据库1 sqlserver

    知识内容: 1.SqlServer数据库概述 2.SqlServer数据库基本操作 3.Transact-SQL程序设计 一.SqlServer数据库概述 1.SqlServer系统概述 SQLSer ...

  6. awk(gawk)

    awk,逐行处理文本内容.Linux里的awk其实是“gawk”. 使用格式: awk [选项] '模式匹配 {命令 命令参数}' file1, file2, …… 支持的选项 说明 -f progr ...

  7. Configure First SpringMVC project in IntelliJ IDEA(fail)

    Configure First SpringMVC project in IntelliJ IDEA 13 The Mechanism of Spring MVC frameworks by Java ...

  8. svn完整搭建

    安装软件 # yum install httpd mod_dav_svn subversion mod_ssl 查看是否安装成功   #svn --version 如果出现版本号如 则说明svn安装成 ...

  9. ssh连接失败,提示 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    [root@iZ2ze4kh1rvftq4cevdfjwZ ~]# ssh IP @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  10. Requests库入门

    安装: $ pip install requests Response对象的一些基本属性: Response.status_code 请求的返回状态,正常为200 Response.text 页面的字 ...