#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(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
""" left=0
right=len(nums)-1
while left <= right:
mid=(left+right)/2
if target == nums[mid]:
return mid
if target > nums[mid]:
left=mid+1
else:
right=mid-1
return left

leetcode Search Insert Position Python的更多相关文章

  1. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. 35. Search Insert Position@python

    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, return the ...

  5. [LeetCode] Search Insert Position 二分搜索

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

  6. [LeetCode] Search Insert Position

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

  7. LeetCode Search Insert Position (二分查找)

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

  8. LeetCode——Search Insert Position

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

  9. [Leetcode] search insert position 寻找插入位置

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

随机推荐

  1. Linux之旅(1): diff, patch和quilt (下)

    Linux之旅(1): diff, patch和quilt (下) 2 quilt 我们自己的项目能够用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件,但又不 ...

  2. springmvc 传递和接收数组参数

    java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...

  3. js 多媒体audio video

    本文主要简单的介绍一下audio 和 video两个标签的用法 <audio src="music.mp3"></audio> <video src= ...

  4. 2014.12.13 ASP.NET文件上传

    一.文件上传:(一)上传到硬盘文件夹1.最简单的上传. [HTML代码] <asp:FileUpload ID="FileUpload1" runat="serve ...

  5. L9-2.安装mysql数据库

    二.安装mysql 1.检查是否安装了mysql 2.安装cmake 输入gmake: make install 安装依赖的软件包: 新建用户权限等: 解压 安装 安装: 安装成功. 安装后调整: v ...

  6. c#操作sqlite

    一.添加选中dll引用如下图 二.下载一个sqlite建表建库工具sqlitedatabasebrowser如下图 三.使用sqlitedatabasebrowser建库建表 四.插入表数据如下图 四 ...

  7. Linux挂载硬盘出错:$LogFile indicates unclean shutdown (0, 0)

    前一次还挂载好好的,今天在挂载NTFS的分区就不行了,出现如下错误信息和提示: $LogFile indicates unclean shutdown (0, 0) Mount is denied b ...

  8. C# 1作业 2广场砖面积 护栏长度

    作业1输入圆柱体的底面半径和高求体积             static void Main(string[] args)         {               //输入圆柱体的底面半径, ...

  9. android anim 动画效果

    动画效果编程基础--AnimationAndroid       动画类型       Android的animation由四种类型组成       XML中    alpha    渐变透明度动画效 ...

  10. 从头开发MUDLIB

    跟Akuma一起从头打造mudlib--[第一讲] 第一讲:让它跑起来注:每一讲我都会上传一个相符的lib,有些文件是旧的,有些是新的,我尽可能在lib里写清楚注释.更详细的内容则在每讲的正文里写. ...