zoukankan      html  css  js  c++  java
  • 74 第一个错误的代码版本

    原题网址:https://www.lintcode.com/problem/first-bad-version/description

    描述

    代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。

    你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。

    请阅读上述代码,对于不同的语言获取正确的调用 isBadVersion 的方法,比如java的调用方式是SVNRepo.isBadVersion(v)

    您在真实的面试中是否遇到过这个题?  

    样例

    给出 n=5

    调用isBadVersion(3),得到false

    调用isBadVersion(5),得到true

    调用isBadVersion(4),得到true

    此时我们可以断定4是第一个错误的版本号

    挑战

    调用 isBadVersion 的次数越少越好

     

    思路:二分法。

    lintcode又抽风,真是给跪了……看看这是什么玩意

    /**
     * class SVNRepo {
     *     public:
     *     static bool isBadVersion(int k);
     * }
     * you can use SVNRepo::isBadVersion(k) to judge whether 
     * the kth code version is bad or not.
    */
    class Solution {
    public:
        /**
         * @param n: An integers.
         * @return: An integer which is the first bad version.
         */
        int findFirstBadVersion(int n) {
            // write your code here
        }
    };
     */
    
    
    class Solution {
    public:
        /*
         * @param n: An integer
         * @return: An integer which is the first bad version.
         */
        int findFirstBadVersion(int n) {
            // write your code here
        }
    };

    以及,这道题隐藏着一个天坑——数据类型的问题,int是不够的,要用long long,不信你试试,只通过90%的数据就超时了。

    个破题提交好多次

    AC代码:

    /**
     * class SVNRepo {
     *     public:
     *     static bool isBadVersion(int k);
     * }
     * you can use SVNRepo::isBadVersion(k) to judge whether 
     * the kth code version is bad or not.
    */
    class Solution {
    public:
        /**
         * @param n: An integers.
         * @return: An integer which is the first bad version.
         */
        int findFirstBadVersion(int n) {
            // write your code here
        if (n<=0)
         {
             return 0;
         }
         long long l=1,r=n;
         while(l<=r)
         {
             long long mid=(l+r)/2;
             if (SVNRepo::isBadVersion(mid))
             {
                 r=mid-1;
             }
             else
             {
                 l=mid+1;
             }
         }
         return l;
        }
    };

    浅谈下自己的理解,已知排序中,false在左,true在右,要找到第一个true。

    二分法,若mid为 false 舍弃左边向右搜索;若mid为 true 舍弃右边向左搜索,直到L > R。

    可以推断出,最后一次循环时 mid 和 L 是相等的,且mid左边都是false,mid右边都是true,但mid本身不一定。所以如果mid为false答案就mid+1,mid为true答案就是mid。

    而退出循环时,要么是R=mid-1,此时mid等于L为true,返回L 或者mid;要么是L=mid+1,此时mid为false,返回mid+1或L,然而无论哪种情况返回L总是没错的。

    参考:

    lintcode-74-第一个错误的代码版本

    https://blog.csdn.net/wangyuquanliuli/article/details/45900057

    https://blog.csdn.net/orekinana/article/details/60138951

     

    以下是我最开始的代码:

    /**
     * class SVNRepo {
     *     public:
     *     static bool isBadVersion(int k);
     * }
     * you can use SVNRepo::isBadVersion(k) to judge whether 
     * the kth code version is bad or not.
    */
    class Solution {
    public:
        /**
         * @param n: An integers.
         * @return: An integer which is the first bad version.
         */
        int findFirstBadVersion(int n) {
            // write your code here
        if (n<=0)
         {
             return 0;
         }
         long long l=1,r=n;
         while(l<=r)
         {
             long long mid=(l+r)/2;
             if (SVNRepo::isBadVersion(mid))
             {
                 if (mid==1)
                 {
                     return mid;
                 }
                 if (mid-1>=1&&SVNRepo::isBadVersion(mid-1)==false)
                 {
                     return mid;
                 }
                 r=mid-1;
             }
             else
             {
                 l=mid+1;
             }
         }
        }
    };

     

     

  • 相关阅读:
    python虚拟环境--virtualenv
    求导法则和高阶导数
    restful规范
    python协程--yield和yield from
    打印质数
    更改docker的存储位置
    同台ECS安装多个mysql
    jenkins + gitlab + harbor + k8s 部署项目
    jenkins 配置maven
    gitlab + jenkins 持续集成
  • 原文地址:https://www.cnblogs.com/Tang-tangt/p/9274501.html
Copyright © 2011-2022 走看看