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


    你是产品经理,目前正在领导一个团队开发一个新产品。不幸的是,您的产品的最新版本没有通过质量检查。由于每个版本都是基于以前的版本开发的,所以在版本差的版本上也是不好的。
    假设你有n个版本[1,2,...,n],你想找出第一个错误的版本,导致下面所有的错误。
    给你一个API布尔isBadVersion(版本),这将返回版本是否坏。实现一个函数来查找第一个错误的版本。您应该尽量减少对API的调用次数。

    1. /* The isBadVersion API is defined in the parent class VersionControl.
    2. bool IsBadVersion(int version); */
    3. public class Solution : VersionControl {
    4. public int FirstBadVersion(int n) {
    5. var res = -1;
    6. var low = 1;
    7. var high = n;
    8. while (low <= high)
    9. {
    10. var mid = low + (high - low) / 2;
    11. if (IsBadVersion(mid))
    12. {
    13. res = mid;
    14. high = mid - 1;
    15. }
    16. else
    17. {
    18. low = mid + 1;
    19. }
    20. }
    21. return res;
    22. }
    23. }








  • 相关阅读:
    【笔记】xml文件读写
    创业唯一不需要的是金钱
    关于阻焊层和助焊层的理解
    UNIX net
    一种方便调试的打印语句宏定义
    C语言指针一种容易错误使用的方法
    文件操作
    MPEG文件格式
    指针在函数间传递实质
    如何查看静态库内容 Unix/Linux
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7912973.html
Copyright © 2011-2022 走看看