Search Insert Position

  • 0

    a convenient way to solve it just in this thinking:find the first value which is larger than the target,and the index of the value is the answer.

    int searchInsert(int* nums, int numsSize, int target) {
        if(numsSize == 0) return 0;
        int j=-1;
        for(int i=0;i<numsSize;i++)
        if(j==-1) j=numsSize;
        return j;

    The time cost is O(n),The space cost is O(1)

Log in to reply

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