题目描述:

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. JavaScript数组的方法

    push()     :将参数加载到数组的最后,返回数组的长度 pop()    :删除数组的最后一个元素,返回删除的值 shift()      :删除数组的第一个元素,返回删除的值 unshift ...

  2. BZOJ1572 工作安排 USACO2009

    描述 Description     Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间.他的工作日从0时刻开始,有100000 ...

  3. oracle---jdbc--laobai

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  4. 个人学习记录2:ajax跨域封装

    /** * 跨域提交公用方法 * @param param 参数 * @param url 跨域的地址 * @param callFun 回调函数 callFun(data) */ function ...

  5. 《Python核心编程》18.多线程编程(二)

    18.1没有线程支持 #!/usr/bin/env python # -*- coding:utf-8 -*- from time import sleep, ctime def loop0(): p ...

  6. 安装MariaDB和简单配置

    1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB systemctl start maria ...

  7. Another app is currently holding the yum lock

    摘要 在使用yum安装的时候,出现该error. 错误 Another app is currently holding the yum lock; waiting for it to exit... ...

  8. ffmpeg常用转换命令,支持WAV转AMR

    音频转换: 1.转换amr到mp3: ffmpeg -i shenhuxi.amr amr2mp3.mp3 2.转换amr到wav: ffmpeg -acodec libamr_nb -i shenh ...

  9. R包igraph探究

    前段时候由于项目的原因,需要画图,然后开始接触R语言的igraph包,网上零零散散的搜罗了不少的信息,放在这边交流分享的同时也给自己留个备份吧~ 1.首先是读取文件,基本选用的都是csv文件 edge ...

  10. MySQL Cluster 集群简介

    简介 MySQL集群是一种在无共享架构(SNA,Share Nothing Architecture)系统里应用内存数据库集群的技术.这种无共享的架构可以使得系统使用低廉的硬件获取高的可扩展性. My ...