题目描述:

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

解题思想:

完完全全的二分嘛

class Solution:

# @param A, a list of integers

# @param target, an integer to be inserted

# @return integer

def searchInsert(self, A, target):

l = len(A)

left = 0

right = l-1

while left <= right:

mid = (left + right) / 2

if A[mid] == target:

return mid

elif A[mid] < target:

left = mid + 1

elif A[mid] > target:

right = mid - 1

return left

s = Solution()
a = [1,3,5,6]
print s.searchInsert(a,5)
print s.searchInsert(a,2)
print s.searchInsert(a,7)
print s.searchInsert(a,0)

【leetcode】Search Insert Position的更多相关文章

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

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

  2. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  3. 【leetcode】 search Insert Position(middle)

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

  4. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  5. 【leetcode】35-Search Insert Position

    problem Search Insert Position 一种容易想到的是暴力破解法,一种是常用的二分法. 暴力破解法1(不推荐) class Solution { public: int sea ...

  6. 【Leetcode】【Medium】Search Insert Position

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

  7. 【数组】Search Insert Position

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

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

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

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

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

随机推荐

  1. C语言拾遗(一)

    越来越体会到C语言的重要性,不管是在计算机底层的理解上,还是在算法数据结构上,所以遂决定重新拾起C语言,不定期更新一些知识点. 推荐博客:http://blog.csdn.net/itcastcpp ...

  2. 移动端meta

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...

  3. 通过grep来进行日志分析,grep -C和配合awk实际对catalina.out使用案例

    本文介绍通过grep来进行日志分析,主要介绍grep -C和配合awk实际对catalina.out使用案例 grep可以对日志文件进行筛选,统计,查询,快速定位bug. 首先,你的日志需要比较规范, ...

  4. 利用Python【Orange】结合DNA序列进行人种预测

    http://blog.csdn.net/jj12345jj198999/article/details/8951120 coursera上 web intelligence and big data ...

  5. .NET学习之路----我对P/Invoke技术的理解(一)

    看了P/Invoke技术的介绍,于是想写下点东西,东西包含两个部分:知识的纪录和我的理解及疑问. r托管代码中调用非托管API函数的过程 1.定位包含API的DLL: 2.载入DLL 3.找到DLL中 ...

  6. ecshop后台 计划任务

    计划任务定时清理掉设置后的内容 主要针对单表删除(日志,):对于多表删除,不太好用(订单+订单商品+订单日志) 结构: 1.计划任务语言包:languages\zh_cn\cron\ 2.php文件: ...

  7. [Unity] Shader(着色器)之固定管线

    在Unity中,固定管线Shader的性能是最好的. 什么是固定管线呢? 固定渲染管线 —— 这是标准的几何&光照(T&L)管线,功能是固定的,它控制着世界.视.投影变换及固定光照控制 ...

  8. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

  9. 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

    错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...

  10. java运行过程

    一.安装环境 大家在开发Java的时候,首先回装一个java的开发环境,一个JDK(也包含了JRE),然后设置环境变量,这个过程我就不细说了,大家装完后有没有发现,在装完这个环境的同时在安装JRE,在 ...