2015年4月14日星期二

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
这道题看着很复杂, 其实就是最简单的binary search 
注意调用isBadVersion时候要用VersionControl.isBadVersion

/**
 * public class VersionControl {
 *     public static boolean isBadVersion(int k);
 * }
 * you can use VersionControl.isBadVersion(k) to judge wether 
 * 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) {
        if (n < 1){
            return -1;
        }
        int start = 1;
        int end = n;
        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (VersionControl.isBadVersion(mid)){
                end = mid;
            } else {
                start = mid;
            }
        }
        if (VersionControl.isBadVersion(start)){
            return start;
        } else if (VersionControl.isBadVersion(end)){
            return end;
        } else {
            return -1;
        }
    }
}

2 条评论:

  1. class Solution(object):
    def firstBadVersion(self, n):
    start, middle, end = 1, 1, n
    while start <= end:
    middle = (start + end) / 2
    if isBadVersion(middle): end = middle - 1
    else: start = middle + 1
    return end + 1

    URL: http://traceformula.blogspot.com/2015/09/first-bad-version-leetcode.html

    回复删除
  2. class Solution(object):
    def firstBadVersion(self, n):
    start, middle, end = 1, 1, n
    while start <= end:
    middle = (start + end) / 2
    if isBadVersion(middle): end = middle - 1
    else: start = middle + 1
    return end + 1

    URL: http://traceformula.blogspot.com/2015/09/first-bad-version-leetcode.html

    回复删除