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.

示例:

[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

问题来源:Search Insert Position (详细地址:https://leetcode.com/problems/search-insert-position/description/)

思路分析:这道题很简单吧,其实就是个二分查找的意思,最后返回的是low指向的索引,因为跳出循环的时候是low > high,所以最后结果应该为low。

代码:

Leetcode之Search Insert Position 问题