Leetcode 002-Search Insert Position

Posted 依然冷月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 002-Search Insert Position相关的知识,希望对你有一定的参考价值。

 1 #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.
 2 def searchInsert(nums, target):
 3     left=0
 4     right=len(nums)-1
 5     while left <=right:
 6         mid=(right-left)/2+left
 7         if nums[mid]==target:
 8             return mid
 9         elif nums[mid]>target:
10             right=mid-1
11         else:
12             left=mid+1
13     return left
14 L=[1,3,5,6]
15 print(searchInsert(L,5))
16 print(searchInsert(L,2))
17 print(searchInsert(L,7))
18 print(searchInsert(L,0))

最快就是用二分法

以上是关于Leetcode 002-Search Insert Position的主要内容,如果未能解决你的问题,请参考以下文章

Attribute "resultType" must be declared for element type "update" or "inser

MySQL 笔记2

oracle 存储过程

计算运行总计/运行余额

StringBuffer类

有关输出对齐