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
这道题太简单了,都不值得一看··············忽略它吧(没想到其实这道题出题者的意思是要用二分搜索的,这里暴力搜索也能通过可能是因为用的是Python,对python的时间复杂度要求低吧。)
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def searchInsert(self, nums, target): for i in range(len(nums)): if target<=nums[i]: return i return len(nums)