Javascript solution


  • 0
    R
    var searchInsert = function(nums, target) {
        if(nums === null) {
            return -1;
        }
        
        left = 0;
        right = nums.length-1;
        while(left <= right) {
            mid = Math.floor((left+right)/2);
            if(nums[mid] === target) {
                return mid;
            }
            else if(target < nums[mid]) {
                right = mid - 1;
            }
            else{
                left = mid + 1;
            }
        }
        
        return left;
    };
    

Log in to reply
 

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