My c solution in 4ms


  • 0
    W

    here is code

    int searchInsert(int* nums, int numsSize, int target) {
        if (nums == NULL || numsSize <= 0) {
            return 0;
        }
    
        int start = 0;
        int end = numsSize - 1;
    
        if (nums[start] > target) {
            return start;
        }
        if (nums[end] < target) {
            return end + 1;
        }
    
        while (start <= end) {
            int middle = start + ((end - start) >> 1);
            if (nums[middle] < target) {
                start = middle + 1;
            } else if (nums[middle] > target) {
                end = middle - 1;
            } else {
                return middle;
            }
        }
    
        return start;
    }

Log in to reply
 

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