LeetCode:Search Insert Position

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Search Insert Position相关的知识,希望对你有一定的参考价值。

Problem:

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

Subscribe to see which companies asked this question

Solution:

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        
        int n=nums.size();
        int i=0;
        
        for(i=0;i<nums.size();i++)
        {
           if(nums[i]>=target) 
                  break;
        }
          return i;
    }
};

 

以上是关于LeetCode:Search Insert Position的主要内容,如果未能解决你的问题,请参考以下文章

leetcode - Search Insert Position

Array——LeetCode——Search Insert Position

[Leetcode] search insert position 寻找插入位置

leetcode Search Insert Position

Leetcode:search_insert_position

leetcode Search Insert Position(easy) /java