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

解析:

这题是为了实现std::lower_bound()

 1 class Solution {
 2 public:
 3     int searchInsert(vector<int>& nums, int target) {
 4         vector<int>::size_type i=0;
 5         for(i;i < nums.size();i++)
 6         {
 7             if(target <= nums[i])
 8                 return i;            
 9         }
10         return i;
11     }
12 };
原文地址:https://www.cnblogs.com/raichen/p/4957958.html