Javascript solution, if anyone is interested


  • 0
    P
    var solution = function(isBadVersion) {
       /**
         * @param {integer} n Total versions
         * @return {integer} The first bad version
         */
        return function(n) {
            var min = 1;
            var max = n;
            var bad = -1;
            while (min <= max) {
                var mid = Math.floor((min+max)/2);
                if (isBadVersion(mid)) {
                    bad = mid;
                    max = mid-1;
                }
                else {
                    min = mid+1;
                }
            }
            return bad;
        };
    };

Log in to reply
 

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