#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.
def searchInsert(nums, target):
left=0
right=len(nums)-1
while left <=right:
mid=(right-left)/2+left
if nums[mid]==target:
return mid
elif nums[mid]>target:
right=mid-1
else:
left=mid+1
return left
L=[1,3,5,6]
print(searchInsert(L,5))
print(searchInsert(L,2))
print(searchInsert(L,7))
print(searchInsert(L,0))

最快就是用二分法

Leetcode 002-Search Insert Position的更多相关文章

  1. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  2. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  3. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

  4. [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 ...

  5. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  7. 【题解】【数组】【查找】【Leetcode】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. [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 ...

  9. 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 ...

  10. [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 ...

随机推荐

  1. mysql 查询死锁语句

    我们可以用下面三张表来查原因:        innodb_trx ## 当前运行的所有事务        innodb_locks ## 当前出现的锁        innodb_lock_wait ...

  2. IPython:一种交互式计算和开发环境

    一 键盘快捷键 - Ctrl-P    或上箭头键 后向搜索命令历史中以当前输入的文本开头的命令 - Ctrl-N   或下箭头键 前向搜索命令历史中以当前输入的文本开头的命令 - Ctrl-R    ...

  3. CodeForces 232E.Quick Tortoise

    John Doe has a field, which is a rectangular table of size n × m. We assume that the field rows are ...

  4. 【CF1015C】Songs Compression(贪心)

    题意: 给定n和m,n组(a[i],b[i]),每一组a[i]可以压缩为b[i],求最少只需要压缩几个,使得m可以存下所有数据,无解输出-1 思路:按差贪心,排序 #include<cstdio ...

  5. 转 Python——UnicodeEncodeError: 'ascii' codec can't encode/decode characters

    转自: http://blog.csdn.net/zuyi532/article/details/8851316 我是写爬虫的时候遇到的问题,百度了一下,先贴解决方案: 在代码中加入: import ...

  6. C和C++内存分配方式记录

    C. C++中内存分配方式可以分为三种: (1)从静态存储区域分配:内存在程序编译时就已经分配好,这块内存在程序的整个运行期间都存在.速度快.不容易出错,因为有系统会善后.例如全局变量,static变 ...

  7. 快充 IC BQ25896 的 input current monitor

    BQ25896 沒有顯示 input current 的 register, 但可以讀 ILIM pin 的 電壓 做計算求出 input current.

  8. Hash Collision DoS 问题

    Hash Collision DoS 问题http://coolshell.cn/articles/6424.html Hash Collision DoS (Hash碰撞的拒绝式服务攻击),有恶意的 ...

  9. Java爬取51job保存到MySQL并进行分析

    大二下实训课结业作业,想着就爬个工作信息,原本是要用python的,后面想想就用java试试看, java就自学了一个月左右,想要锻炼一下自己面向对象的思想等等的, 然后网上转了一圈,拉钩什么的是动态 ...

  10. Java -----transient 和static

    越来越喜欢深究java基础了,讲讲 transient  和static 对序列化的影响.废话少说,直接上代码就可以了 package serializable; import java.io.Ser ...