zoukankan      html  css  js  c++  java
  • Lintcode: First Bad Version 解题报告

    First Bad Version

    http://lintcode.com/en/problem/first-bad-version

    The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests.

    You can determine whether a version is bad by the following interface: 

     

    Java:
        public VersionControl {
            boolean isBadVersion(int version);
        }
    C++:
        class VersionControl {
        public:
            bool isBadVersion(int version);
        };
    Python:
        class VersionControl:
            def isBadVersion(version)

     

    Find the first bad version.

    Note

    You should call isBadVersion as few as possible. 

    Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion.

    Example

    Given n=5

    Call isBadVersion(3), get false

    Call isBadVersion(5), get true

    Call isBadVersion(4), get true

    return 4 is the first bad version

    Challenge

    Do not call isBadVersion exceed O(logn) times.

    Tags Expand

    SOLUTION 1:

    九章算法模板解法,注意,一定要使用left + 1 < right 作为while的条件,这样子不会产生死循环和越界的情况。
     1 /**
     2  * public class VersionControl {
     3  *     public static boolean isBadVersion(int k);
     4  * }
     5  * you can use VersionControl.isBadVersion(k) to judge wether 
     6  * the kth code version is bad or not.
     7 */
     8 class Solution {
     9     /**
    10      * @param n: An integers.
    11      * @return: An integer which is the first bad version.
    12      */
    13     public int findFirstBadVersion(int n) {
    14         // write your code here
    15         if (n == 1) {
    16             return 1;
    17         }
    18         
    19         int left = 1;
    20         int right = n;
    21         
    22         while (left + 1 < right) {
    23             int mid = left + (right - left) / 2;
    24             if (VersionControl.isBadVersion(mid)) {
    25                 right = mid;
    26             } else {
    27                 left = mid;
    28             }
    29         }
    30         
    31         if (VersionControl.isBadVersion(left)) {
    32             return left;
    33         }
    34         
    35         return right;
    36     }
    37 }
    View Code

    SOLUTION 2:

    也可以简化一点儿:

     1 /**
     2  * public class VersionControl {
     3  *     public static boolean isBadVersion(int k);
     4  * }
     5  * you can use VersionControl.isBadVersion(k) to judge wether 
     6  * the kth code version is bad or not.
     7 */
     8 class Solution {
     9     /**
    10      * @param n: An integers.
    11      * @return: An integer which is the first bad version.
    12      */
    13     public int findFirstBadVersion(int n) {
    14         // write your code here
    15         if (n == 1) {
    16             return 1;
    17         }
    18         
    19         int left = 1;
    20         int right = n;
    21         
    22         while (left < right) {
    23             int mid = left + (right - left) / 2;
    24             if (VersionControl.isBadVersion(mid)) {
    25                 right = mid;
    26             } else {
    27                 left = mid + 1;
    28             }
    29         }
    30         
    31         return right;
    32     }
    33 }
    View Code
  • 相关阅读:
    codeforces 650B
    2013 ACM区域赛长沙 H zoj 3733 (hdu 4798) Skycity
    2013 ACM区域赛长沙 I LIKE vs CANDLE(ZOJ3734) 很好的一道树形DP
    Codeforces Round #306 (Div. 2)——A——Two Substrings
    Codeforces Round #306 (Div. 2)——B暴力——Preparing Olympiad
    HDU5248——二分查找——序列变换
    HDU2255——KM算法——奔小康赚大钱
    匈牙利算法&KM算法
    HDU1059——多重部分和问题——Dividing
    HDU1058——Humble Numbers
  • 原文地址:https://www.cnblogs.com/yuzhangcmu/p/4161837.html
Copyright © 2011-2022 走看看