zoukankan      html  css  js  c++  java
  • 278. First Bad Version

    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 /* The isBadVersion API is defined in the parent class VersionControl.
     2       boolean isBadVersion(int version); */
     3 
     4 public class Solution extends VersionControl {
     5     public int firstBadVersion(int n) {
     6         int i = 1, j = n;
     7         int mid = -1;
     8         
     9         while (i < j) {
    10             mid = (j - i) / 2 + i;
    11             if (isBadVersion(mid)) j = mid;
    12             else i = mid + 1;
    13         }
    14         return i;
    15     }
    16 }
  • 相关阅读:
    UML基本关系
    C++关键字简述
    Install opencv on Centos
    C++编程规范
    YCbCr to RGB and RGB toYCbCr
    Linux目录结构(二)
    Dubbo工作流程
    spring bean的作用域和生命周期
    spring aop原理和实现
    静态代理、jdk动态代理、cglib动态代理
  • 原文地址:https://www.cnblogs.com/joycelee/p/5340694.html
Copyright © 2011-2022 走看看