• 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. }








  • 相关阅读:
    毕设2020.02.02
    架构师眼中的高并发架构
    云时代架构读后感二
    以《淘宝网》为例,描述质量属性六个常见场景
    《架构漫谈》读后感
    云时代架构读后感一
    暑假周总结报告08
    暑假周总结报告07
    暑假周总结报告06
    假期周总结报告05
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7912973.html
Copyright © 2020-2023  润新知