C++ O(logn) 7 lines


  • 0
    A

    Move two indexes until they meet, and return the index.

        int firstBadVersion(int n) {
            int p = 1;
            while (p < n) {
                int mid = p + (n-p)/2;
                if (isBadVersion(mid)) n = mid;
                else p = mid + 1;
            }
            return p;
        }
    

Log in to reply
 

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