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








  • 相关阅读:
    git常用命令大全
    谷粒商城遇到的问题
    谷粒商城Seata(四十一)
    通过VMware Horizon Client访问虚拟机
    Qt 让窗口(或控件)居中
    QT 设置QDockWidget的初始大小
    Qt QDockWidget小结
    Qt QDockWidget停靠窗相关的信号
    Qt 基于Qt的词典开发系列--无边框窗口的缩放与拖动
    Qt 创建停靠悬浮窗口 QDockWidget
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7912973.html
Copyright © 2011-2022 走看看