Intuitive and Simple C solution


  • 0
    S

    int searchInsert(int* nums, int numsSize, int target)
    {
    if(numsSize==0) {return 0;}

    if(numsSize ==1)
    {
        if(nums[0]==target) return 0;
        if(nums[0]<target) return 1;
        return 0;
    }
    
    if(numsSize==2){
        if(nums[0]==target) return 0;
        if(nums[1]==target) return 1;
        if(nums[0]>target) return 0;
        if(nums[1]<target) return 2;
        if(nums[0]<target && target<nums[1]) return 1;
    }
    
    
    int mid = numsSize/2;
    if(nums[mid]==target) return mid;
    if(nums[mid]<target)
    {
        return mid+1+searchInsert(nums+mid+1, numsSize-mid-1, target);
    }
    return searchInsert(nums, numsSize-mid, target);
    

    }


Log in to reply
 

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