Two different solutions in C accepted with 4ms and 0ms respectively


  • 0
    //AC - 4ms;
    int searchInsert(int* nums, int numsSize, int target)
    {
        int i = 0;
        while(i < numsSize)
            if(nums[i++] >= target)
                break;
        return (target > nums[i-1])? i : i-1;
    }
    

    //AC - 0ms;
    int searchInsert(int* nums, int numsSize, int target)
    {
        int low = 0, high = numsSize - 1;
        int mid;
        while(low <= high)
        {
            mid = (low+high)/2;
            if(nums[mid] == target)
                return mid;
            else if(nums[mid] > target)
                high = mid - 1;
            else 
                low = mid + 1;
        }
        return low;
    }

Log in to reply
 

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