二分法查找需要插入的位置,需要注意两点

1、如果元素不存在,停止的时候start Index刚好是需要插入的位置

2、如果元素存在,需要向前追溯找到非目标元素的起始边界

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'qqvipfunction' class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
""" return self.binary_search(nums, 0, len(nums) - 1, target) #二分查找算法
def binary_search(self, nums, start, end, targrt):
if start > end:
print(start, end)
return start if start > 0 else 0
mid = start + (end - start)//2
if nums[mid] > targrt:
return self.binary_search(nums, start, mid - 1, targrt)
elif nums[mid] < targrt:
return self.binary_search(nums, mid + 1, end, targrt)
else:
index = mid
for i in range(mid - 1, -1, -1):
if nums[i] == targrt:
index = i;
else:
break
return index if __name__ == '__main__':
s = Solution()
print(s.searchInsert([1], 0))

  

Leetcode-34-Search Insert Position-(Medium)的更多相关文章

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

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

  2. leetcode 34 Search Insert Position

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

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

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

  4. LeetCode 035 Search Insert Position

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

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

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

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

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

  8. 【题解】【数组】【查找】【Leetcode】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. 分布式文件系统之MogileFS工作原理及实现过程

    MogileFS是一套高效的文件自动备份组件,由Six Apart开发,广泛应用在包括LiveJournal等web2.0站点上.MogileFS由3个部分组成:   第1个部分:是server端,包 ...

  2. Javascript实例技巧精选(8)—计算当月剩余天数

    >>点击这里下载完整html源码<< 截图如下: 利用Javascript在网页上计算当前月份的剩余天数,相应代码如下: <script language="J ...

  3. [每日一题] OCP1z0-047 :2013-08-01 正则表达式--- REGEXP_REPLACE 函数

    这题又是考正则表达式,我们先根据题意,操作如下: hr@OCM> col "PHONE NUMBER" for a50 hr@OCM> SELECT phone_num ...

  4. 跨域访问 REST API

    跨域访问 Web Service (REST API) 虽然 JQuery 也能通过授权header实现跨域, 但SharePoint 提供了更简单的方法,它被实现在SP.RequestExecuto ...

  5. java中的log

    slf4j slf4j的全称是:  Simple Logging Facade for Java (SLF4J). slf4j的官方网站:http://www.slf4j.org 简介 SLF4J不是 ...

  6. 分享Syslinux4USB 0.3源码——改自神雕大侠作品

    神雕大侠写的Syslinux4USB是我在无忧论坛里找到的工具,他是2011年写出的,他在帖子里也发布了源码,可惜那个帖子里的链接失效了,我为了这个工具的源码找了2年,终于在Google上搜到了,并且 ...

  7. 自定义ModelValidatorProvider

    MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息 Asp.net MVC中的提供非常简单易用的数据验证解决方案. 通过System.Compone ...

  8. [转]在 Mac OS X上编译 libimobiledevice 的方法

    link: http://blog.boceto.fr/2012/05/05/libimobiledevice-for-macosx/ The objective of the day: Compil ...

  9. Lazy<T>

    Lazy<T> 对象的创建方式,始终代表了软件工业的生产力方向,代表了先进软件技术发展的方向,也代表了广大程序开发者的集体智慧.以new的方式创建,通过工厂方法,利用IoC容器,都以不同的 ...

  10. Python-数据库支持

    10.Python-数据库支持 使用数据库的好处: a.支持数据的并发访问,多个用户同时对基于磁盘的数据进行读写而不造成任何文件的损坏: b.支持根据多个数据字段或属性进行复杂的搜索: 1.如何操作数 ...