3b618cd113f5985c1058c1abe581da1d
(难度Easy)Problem 35. Search Insert Position(寻找插入位置)

链接

Search Insert Position - LeetCode

难度等级

Easy

题干

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.

题意

给定一个有序的数组,和一个元素target。如果target在数组当中则返回target的位置,否则返回target可以插入的位置。

样例

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

题解

做过之前两题的同学,这道题的确很简单。

top Created with Sketch.