My easy javascript solution actualy in O(logn)


  • 0
    L
    function searchrange  (nums, target) {
    var result = [-1, -1];
    var le = 0;
    var hr = nums.length;
    var binarysearch = function findTarget(A, l, h, target) {
    	if (l > h) {
    		return -1;
    	}
    	var mid =(h + l)/2;
    	var mid =  Math.floor(mid)
    	if (A[mid] === target) {
            console.log(l,h);
    		return mid;
    	}
    	if (target < A[mid]) {
    		return findTarget(A, l , mid-1, target);
    	}else {
    		return findTarget(A, mid+1, h, target);
    	}
    }
    var res = binarysearch(nums, le, hr, target);
    if (res === -1) {
    	return [-1, -1];
    }else {
    	var left = res;
    	var right = res;
    	while (left > -1) {
    		result[0] = left;
    		left = binarysearch(nums, le, left - 1, target);
    	}
    	while (right > -1) {    		
    		result[1] = right;
    		right = binarysearch(nums, right + 1, hr, target);
    	}
    	return result;
    }
    

    };


Log in to reply
 

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