# LeetCode 35. 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

 int searchInsert(vector<int>& nums, int target)
{
int n = nums.size();

int low = 0;
int high = n -1;
int mid;
while(low <= high)
{
mid = low + (high - low)/2;
if(target == nums[mid])
return mid;
else if(target < nums[mid])
high = mid - 1;
else
low = mid + 1;

}
return low;
}

#### LeetCode35——Search Insert Position

2015-10-15 22:01:06

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

2015-01-24 15:07:12

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

2016-08-07 21:46:08

#### leetcode刷题记录-35. Search Insert Position

2018-03-21 20:30:24

#### 35. Search Insert Position

2017-03-13 23:34:02

#### LeetCode35. Search Insert Position轻松解决

2016-02-21 21:14:12

#### Search Insert Position -- LeetCode

2014-03-02 07:40:19

#### LeetCode 35 Search Insert Position（搜索并插入）

2015-11-28 21:16:01

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

2015-05-18 20:33:01

#### LeetCode — Search Insert Position

2018-01-05 15:20:32