C++ 5 lines 0ms binary search


  • 1
        int firstBadVersion(int n) {
           int lower = 1, upper = n;
           while(lower < upper)
               if(isBadVersion(lower + (upper - lower)/2)) upper = lower + (upper - lower)/2;
               else lower = lower + (upper - lower)/2 + 1;
           return lower;
        }
    

Log in to reply
 

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