Binary search in c++


  • 2
    F

    search until you find the first bad version.

    bool isBadVersion(int version);
    
    class Solution {
    public:
        int firstBadVersion(int n) {
            int start = 1, end = n;
            while (start < end) {
                int mid = start + (end - start)/2;
                if (isBadVersion(mid)) end = mid;
                else start = mid + 1;
            }
            return end;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.