lintcode :First bad version 第一个错误的代码版本

题目

第一个错误的代码版本

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

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

样例

给出 n=5

调用isBadVersion(3),得到false

调用isBadVersion(5),得到true

调用isBadVersion(4),得到true

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

注意

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

挑战

调用 isBadVersion 的次数越少越好

解题

感觉顺序遍历,找到第一个出现true的就是答案了,但是超时了。只有二分查找了

/**
 * public class SVNRepo {
 *     public static boolean isBadVersion(int k);
 * }
 * you can use SVNRepo.isBadVersion(k) to judge whether 
 * the kth code version is bad or not.
*/
class Solution {
    /**
     * @param n: An integers.
     * @return: An integer which is the first bad version.
     */
    public int findFirstBadVersion(int n) {
        // write your code here
        if(n<=0)
            return 0;
        if(n==1)
            return 1;
        int left = 1;
        int right = n;
        while(left  < right){
            int mid = (left + right)/2;
            if(SVNRepo.isBadVersion(mid))
                right = mid;
            else
                left = mid + 1;
        }
        return left;
    }
}
Java Code
#class SVNRepo:
#    @classmethod
#    def isBadVersion(cls, id)
#        # Run unit tests to check whether verison `id` is a bad version
#        # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a 
# bad version.
class Solution:
    """
    @param n: An integers.
    @return: An integer which is the first bad version.
    """
    def findFirstBadVersion(self, n):
        # write your code here
        if n<=0:return 0
        if n==1:return 1
        l = 1
        r = n
        while l< r:
            m = (l + r)/2
            if SVNRepo.isBadVersion(m):
                r = m
            else:
                l = m + 1
        return r 
Python Code
原文地址:https://www.cnblogs.com/bbbblog/p/5128604.html