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 }
  • 相关阅读:
    invokerequied
    C#如何用正则表达式截取https和带端口的域名
    flex datagrid combox
    java webservice
    Flexlib 控件 http://code.google.com/p/flexlib/
    Git使用
    转:Vim常用命令
    转:oracle连接查询
    Axis2 spring发布WebService
    oracle jdbc example 示例
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511697.html
Copyright © 2011-2022 走看看