# Search Insert Position

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

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
// 处理特殊情况，nums为空和target小于最小的或者大于最大的数
if(nums.empty() || target < nums[0])
return 0;
int numsSize = nums.size();
if(nums[numsSize - 1] < target)
return numsSize;
int left = 0, mid, right = numsSize - 1;
while(left < right - 1) {
mid = (left + right) / 2;
if(nums[mid] > target)
right = mid;
else if(nums[mid] < target)
left = mid;
else
return mid;
}
if(nums[left] == target)
return left;
return right;
}
};

#### Search Insert Position -- LeetCode

2014-03-02 07:40:19

#### LeetCode解题报告--Search Insert Position

2015-12-04 16:58:11

#### Python - Search Insert Position

2016-01-16 19:52:32

#### 【LeetCode-面试算法经典-Java实现】【035-Search Insert Position（搜索插入位置）】

2015-07-27 07:18:33

#### LeetCode35——Search Insert Position

2015-10-15 22:01:06

#### (Java)LeetCode-35. Search Insert Position

2016-08-07 21:46:08

#### Search Insert Position三种方法解析

2017-02-23 15:21:26

#### LeetCode 35 Search Insert Position (C,C++,Java,Python)

2015-05-18 20:33:01

#### 搜索插入位置：Search Insert Position

2016-06-17 12:14:47

#### [LeetCode]35.Search Insert Position

2015-01-24 15:07:12