题目描述:

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. 【Codeforces717G】Underfail Hash + 最大费用最大流

    G. Underfail time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  2. Javascript和HTML:

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  3. BUAA_OVERWATCH第一次行动前战略部署

    这太IMBA了! 需求调研问卷的反馈 #define A 调查问卷 A设计背景 随着各种新兴手游的兴起,以及各大直播间内Lying Man的火热,以及各种娱乐方式的发展,传统桌游很好地移植到app上的 ...

  4. mysql习惯及主从复制参数设置

    mysql 重复数据插入 replace into t(id, update_time) values(1, now()); 或 replace into t(id, update_time) sel ...

  5. 69个经典Spring面试题和答案

    Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标是简化Java企业级应用开发,并通过PO ...

  6. [转]eclipse快捷键

    Ctrl+1 快速修复Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当前行和下面一行交互位置(特别实用 ...

  7. <<< html5本地储存

    类似与Cookies,但由于Cookies储存量太小,大小也只有4-5KB的样子,html5的本地储存能存5M大小的数据 html5本地储存属性有,localStorage和sessionStorag ...

  8. (转)理解MySQL——索引与优化

    参考资料:http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html ———————————— 全文: 写在前面:索引对查询的速度有着 ...

  9. webpack-dev-server

    webpack-dev-server是一个小型的node.js Express服务器,它使用webpack-dev-middleware中间件来为通过webpack打包生成的资源文件提供Web服务.它 ...

  10. Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found

    1.get the rvm version rvm -v 2.make sure the version at least 1.26 above. 3.then go ahead rvm get he ...