Javascript recursive solution


  • 0
    R
    var solution = function(isBadVersion) {
        return function(n) {
            var helper=function( n,  start,  end) {
                if(start>=end) {
                    return start;
                }
                var middle = Math.floor(start + (end - start) / 2);
                if(isBadVersion(middle)) {
                    return helper(n, start, middle);
                } else {
                    return helper(n, middle + 1, end);
                }
            };
            if(n == 0) {
                return 0;
            }    
            return helper(n, 1, n);
        };
    };
    
    
    

Log in to reply
 

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