Leetcode题目:Search Insert Position

题目:

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

题目解答:

这个题目是需要返回数组中应该插入当前target这个数字的合适位置。可以使用STL中的lower_bound这个函数来完成查找。

代码:

1 class Solution {
2 public:
3     int searchInsert(vector<int>& nums, int target) {
4         vector<int>::iterator vit = lower_bound(nums.begin(), nums.end(), target);
5         return vit - nums.begin();
6     }
7 };
原文地址:https://www.cnblogs.com/CodingGirl121/p/5549479.html