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 }
  • 相关阅读:
    [转] 64位Oracle 11g R2的客户端连接时报ORA-01019错误
    [转]Oracle11g链接提示未“在本地计算机注册“OraOLEDB.Oracle”解决方法
    [转]通过Net Manager 配置Oracle 11g本地监听服务(listener service)
    [转]jQuery Mobile动态刷新页面样式
    [转]SSIS
    [转]在SSIS中,使用“包配置”时的常见错误与解析
    使用JMH做Java微基准测试
    秒杀系统解决方案
    cookie机制和session机制
    app如何节省流量
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511697.html
Copyright © 2011-2022 走看看