zoukankan      html  css  js  c++  java
  • 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.

    Analyse: find a number k in an continuous set {1, 2, ..., n}. Binary search.

    Runtime: 0ms.

     1 // Forward declaration of isBadVersion API.
     2 bool isBadVersion(int version);
     3 
     4 class Solution {
     5 public:
     6     int firstBadVersion(int n) {
     7         if(n == 0) return 0;
     8         if(n == 1) return isBadVersion(1);
     9         
    10         long long low = 0, high = n;
    11         while(low < high){
    12             long long mid = (low + high) >> 1;
    13             if(isBadVersion(mid)) high = mid;
    14             else low = mid + 1;
    15         }
    16         return high;
    17     }
    18 };
  • 相关阅读:
    Java守护线程Daemon
    在for循环中创建双向链表
    Java泛型-官方教程
    大自然搬运工
    转 curl命令
    HashMap扩容问题及了解散列均分
    mysql 分组查询并取出各个分组中时间最新的数据
    CNN 模型复杂度分析
    Attention机制
    深度学习之目标检测
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4796678.html
Copyright © 2011-2022 走看看