Use binary search with C


  • 0
    F

    int searchInsert(int* nums, int numsSize, int target) {

    int mid;
    int l = 0;
    int r = numsSize - 1;
    
    while(l <= r) {
        mid = (l+r)/2;
        
        if(nums[mid] == target) {
            return mid;
        }        
        
        if(target < nums[mid]) {
            r = mid - 1;        
        } else {
            l = mid + 1;
        }; 
    }
    
    return l;
    

    }


Log in to reply
 

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