C++ solution,easy to understand,0(n) time complexity,7lines


  • 0
    int searchInsert(vector<int>& nums, int target) {
        int n=nums.size();
        if(target>nums[n-1])return n;
        int pos=n-1;
        for(int i=n-2;i>=0;i--){
            if(target<=nums[i])
                pos--;
        }
        return pos;
    }

Log in to reply
 

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