题目:

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.

思路:

  • 题意:给定1~n个数字代表产品,其中一个产品坏了,后面的全坏,要求检测第一个坏的(提供了函数)
  • 二分法,这里的二分法判断条件比较特殊,当然后面的设置变量要用long,没想明白

代码:

/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
         long begin = 1;
        long end = n;
        if(n<1) return 0;
        while(begin<end){
            long mid = (begin+end)/2;
            if(isBadVersion((int)mid)){
                end = mid-1;
            }else{
                begin = mid+1;
            }
        }
        if(isBadVersion((int)begin)) return (int)begin;
        return (int)begin+1;
    }
}

LeetCode(63)-First Bad Version的更多相关文章

  1. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  2. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  3. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). 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. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

  6. leetcode 278. First Bad Version

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

  7. (medium)LeetCode 278.First Bad Version

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

  8. Leetcode 278 First Bad Version 二分查找(二分下标)

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

  9. 【LeetCode】165 - Compare Version Numbers

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

随机推荐

  1. Android开源框架ViewPagerIndicator的基本使用

    转载本博客请注明出处:点击打开链接    http://blog.csdn.net/qq_32059827/article/details/52495647 很多新闻资讯类的app都有一些共性,那就是 ...

  2. 深入浅出seesion和cookie

    session在计算机中,尤其是在网络应用中,称为"会话控制".session 对象存储特定用户会话所需的属性及配置信息.session跟踪是Web程序中常用的技术,用来跟踪用户的 ...

  3. RocketMQ,JStorm与Tair使用笔记

    关于RocketMQ 启动mq nohup sh mqnamesrv -n 10.150.0.94:9876 &  nohup sh mqbroker -n 10.150.0.94:9876 ...

  4. 巨星陨落 - Jim Gary

    偶然在微软Research中搜论文时搜到了神牛Jim Gary的paper,看着照片有点眼熟,貌似在买过的哪本书中见过.于是就饶有兴致地看着Jim的生平介绍,结果-  "Dr. Gray j ...

  5. 04 SimpleAdapter

    <span style="font-size:18px;">package com.fmyboke; import java.util.ArrayList; impor ...

  6. Portlet开发入门实例

    1原生Portlet开发 这是最简单.最本质的开发方式,直接基于Portlet规范定义的接口开发Portlet.优点是贴近底层比较灵活, 缺点当然就是所有事情都要自己去做.就好比不用SpringMVC ...

  7. 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  8. Oracle WorkFlow(工作流)(二)

    2.4消息(Message) 消息主要是为通知服务的,可以把消息当作通知的内容和类型.消息也属于一个单据类型,通知只能和同一个单据类型里的消息相关联. 每个消息可以有一个或多个属性和自己相联系,消息的 ...

  9. 12、Libgdx的图像之全屏和垂直同步

    (官网:www.libgdx.cn) 检测当前设置 判断是否设置全屏,可以通过如下方式: boolean fullscreen = Gdx.graphics.isFullscreen(); 设置全屏和 ...

  10. oracle的rownum与having用法 去除重复 在重复情况用rownum

    一般来说,大家会用rownum,也就是伪列来指定要显示多条数据, 比如 select linename from aced where rownum<3 但是,大家注意,如果取出来的数据有重复数 ...