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 }
  • 相关阅读:
    Android 判断现在系统存储器是“手机存储”还是“SD存储”
    Android中Is library配置的作用
    [已解决]报错: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Users/mac/Ana
    [未解决]报错:ssh_exchange_identification: read: Connection reset by peer
    Python快速设置Excel表格边框
    Python字典排序
    Scrapy框架: Request回调函数
    Scrapy框架: 异常错误处理
    Scrapy框架: 通用爬虫之SitemapSpider
    Scrapy框架: 通用爬虫之CSVFeedSpider
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511697.html
Copyright © 2011-2022 走看看