simple C solution,3 lines,3 ms


  • 0
    W
    int searchInsert(int* nums, int numsSize, int target) {
        for(int i=0;i<numsSize;i++){
        	if(target<=nums[i]) return i;
        }
        return numsSize;
    }
    

  • 0
    W

    a new simple C solution with Binary Search,4 lines

    int searchInsert(int* nums, int numsSize, int target) {
        if(numsSize==0) return 0;
        if(target==nums[numsSize/2]) return numsSize/2;
        else if(target<nums[numsSize/2]) return searchInsert(nums,numsSize/2,target);
        else return numsSize/2+1+searchInsert(nums+numsSize/2+1,ceil(numsSize/2.0)-1,target);
    }
    

Log in to reply
 

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