First Bad Version

code


// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        if(n == 0)
        {
            return 0;
        }

        int s = 1;
        int e = n;

        while(s + 1 < e)
        {
            int mid = s+ (e-s)/2;
            if(isBadVersion(mid))
            {
                e = mid;
            }
            else
            {
                s = mid;
            }
        }

        if(isBadVersion(s))
        {
            return s;
        }
        else
        {
            return e;
        }
    }
};

results matching ""

    No results matching ""