C Clever Solution Without Binary Search 4ms


  • -3
    P
    int searchInsert(int* nums, int numsSize, int target) {
    
    if(numsSize == 0 || nums == NULL)
        return 0;
    
    if(nums[0] >= target) return 0;
    else if(nums[numsSize-1] < target) return numsSize;
    
    int i;
    int lesser = 0;
    for(i=0;i<numsSize;i++)
    {
        if(nums[i] == target) return i;
        if(nums[i] <= target) lesser = i;
        if(nums[i] > target) break;
        
        
    }
    return lesser+1;
    

    }


  • 1
    M
        for (int i = 0; i < numsSize; i++)
        {
            if (nums[i] >= target) return i;
        }
        return numsSize;
    

    does the same thing


Log in to reply
 

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