zoukankan      html  css  js  c++  java
  • LeetCode 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.
    ince 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 /*************************************************************************
     2     > File Name: LeetCode278.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Thu 19 May 2016 20:01:47 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     First Bad Version
    11     
    12     You are a product manager and currently leading a team 
    13     to develop a new product. Unfortunately, the latest version of 
    14     your product fails the quality check. 
    15     ince each version is developed based on the previous version, 
    16     all the versions after a bad version are also bad.
    17 
    18     Suppose you have n versions [1, 2, ..., n] and you want to find out 
    19     the first bad one, which causes all the following ones to be bad.
    20 
    21     You are given an API bool isBadVersion(version) 
    22     which will return whether version is bad. 
    23     Implement a function to find the first bad version. 
    24     You should minimize the number of calls to the API.
    25 
    26  ************************************************************************/
    27 
    28 #include "stdio.h"
    29 
    30 // Forward declaration of isBadVersion API.
    31 bool isBadVersion(int version);
    32 
    33 int firstBadVersion(int n) 
    34 {
    35     int low = 1;
    36     int high = n;
    37     int middle;
    38     
    39     while( low <= high )
    40     {
    41         middle = low + ( high - low ) / 2;        //此处一定要用low+(high-low)/2 如果使用(low+high)/2 可能溢出
    42         if( isBadVersion(middle) == true )
    43         {
    44             high = middle - 1;
    45         }
    46         else
    47         {
    48             low = middle + 1;
    49         }
    50     }
    51     return low;
    52 }
  • 相关阅读:
    转:[Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制TreeView节点展开?
    C#线程同步方法——Monitor
    转:Mongodb源码分析之Replication模式
    转:Mysql使用主从复制机制(replication)
    Ruby IDE
    转:ASP.NET MVC4细嚼慢咽---(5)js css文件合并
    转:ASP.NET MVC4细嚼慢咽---(6)全局过滤器
    转:WCF服务开发与调用的完整示例
    转:WF工作流技术内幕 —— 通过Web服务调用Workflow工作流(开发持久化工作流)
    汇总高效的卷积神经网络结构[转载]
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511697.html
Copyright © 2011-2022 走看看