题目:

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初级教程:如何自定义一个状态选择器

    有这样一种场景:点击一下某个按钮或者图片(view),改变了样式(一般改变背景颜色).这个时候一种解决方案,可能就是状态选择器.接下来就介绍如何实现状态选择器: 步骤: 一.新建这样的文件夹:res/ ...

  2. 在javascript里 string 和 int 类型转换

    string 转换为int 类型 (1)tostring()方法 var   x=10    a   =   x.toString() //输出为string类型 alert(typeof(a)); ...

  3. tomcat配置集群

    在Tomcat中使用集群功能相对简单.最简单的用法是直接在server.xml文件的或节点下添加 <Cluster className="org.apache.catalina.ha. ...

  4. Android开发学习之路--Activity之Intent

    窗外再次飘起了小雪,还有1周就过年了,2016年即将到来,来年不知道自己将身处何处,船到桥头自然直吧.还是继续学习吧,上次学习了Activity,那么如果是两个Activity之间,怎么从一个Acti ...

  5. HDFS追本溯源:HDFS操作的逻辑流程与源码解析

    本文主要介绍5个典型的HDFS流程,这些流程充分体现了HDFS实体间IPC接口和stream接口之间的配合. 1. Client和NN Client到NN有大量的元数据操作,比如修改文件名,在给定目录 ...

  6. 总账balance表

    SELECT gb.period_net_dr, --期间发生额        gb.period_net_cr, --期间发生额        gb.project_to_date_dr, --账户 ...

  7. GDAL书籍

    GDAL的书籍经过快两年的编写修改,终于出版发行了,有需要的同学可以到下面的网址进行购买. 购买地址: 亚马逊:http://www.amazon.cn/GDAL%E6%BA%90%E7%A0%81% ...

  8. (二十六)静态单元格(Cell)

    制作类似iOS系统设置的页面,如果使用代码来实现,将会比较麻烦,可以通过静态单元格技术方便的实现. 注意:静态单元格只支持TableViewController. 可以通过storyboard直接操作 ...

  9. Dynamics Crm2011 Removes an option from an Option Set control

    应用场景:OptionSet中有N个option值,特定的条件下需要去除某些option的显示,例如在某个条件下我要红框中的两个option不显示 var purchasetype= Xrm.Page ...

  10. 华为机试题【13】-wave数组找字母游戏

    题目描述: Word Maze 是一个网络小游戏,你需要找到以字母标注的食物,但要求以给定单词字母的顺序吃掉.如上图,假设给定单词if,你必须先吃掉i然后才能吃掉f. 但现在你的任务可没有这么简单,你 ...