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.

Example 1:                      Input: [1,3,5,6], 5                        Output: 2

Example 2:                      Input: [1,3,5,6], 2                        Output: 1

Example 3:                      Input: [1,3,5,6], 7                        Output: 4

Example 4:                      Input: [1,3,5,6], 0                        Output: 0

思路


这道题最简单的思路就是从头开始查找,直到找到第一个大于等于target的位置。时间复杂度为O(n),空间复杂度为O(1)。

  第二种思路就是利用二分查找的思路,我们在数组中找到第一个大于等于target的位置,然后就是插入位置。时间复杂度为O(log n), 空间复杂度为O(1)。

解决代码


 class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if len(nums) < 1:
return
start, end = 0, len(nums)-1 while start <= end:
mid = start +((end -start)>>1)
if nums[mid] < target: # nums[middle]小于target时,移动指针位置.
start = mid+1
else: # 当我们找到等于或者大于target时,判断以下当前下标位置以及,上一个元素是否小于target
if mid == 0 or nums[mid-1] <target:
return mid
end = mid -1
return start # 返回开始位置

【LeetCode每天一题】Search Insert Position(搜索查找位置)的更多相关文章

  1. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  2. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  3. [LeetCode] Search Insert Position 搜索插入位置

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

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

  5. 【算法】LeetCode算法题-Search Insert Position

    这是悦乐书的第152次更新,第154篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35).给定排序数组和目标值,如果找到目标,则返回索引. 如果没有, ...

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

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

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

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

  8. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

  9. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

随机推荐

  1. es7 class装饰器

    文档http://es6.ruanyifeng.com/#docs/decorator ts文档 https://www.tslang.cn/docs/handbook/decorators.html ...

  2. 杭电ACM 1297 Children’s Queue

    这道题是排序问题,可以用递归方法解决. 计算F(n): 一:当最后一个是男孩M时候,前面n-1个随便排出来,只要符合规则就可以,即是F(n-1): 二:当最后一个是女孩F时候,第n-1个肯定是女孩F, ...

  3. easyui---layout实战

    第一步: <div id="cc" class="easyui-layout" fit=true> <div region="nor ...

  4. GEM5安装

    转自:http://blog.sina.com.cn/s/blog_548b0a230101cagk.html GEM5在ubuntu13.10下安装过程,首先下载gem5_stable最新安装包,解 ...

  5. python字典查询功能

    def fetch(data): print(' 查询功能') print('用户数据是',data) def add(): pass def change(): pass def delete(): ...

  6. Writable序列化

    序列化:将内存中的对象 转换成字节序列以便于存储在磁盘上或者用于网络传输. 反序列化:将磁盘或者从网络中接受到的字节序列,装换成内存中的对象. 自定义bean对象(普通java对象)要想序列化传输,必 ...

  7. shell脚本的基本结构以及如何执行

    1.shell脚本通常以.sh为后缀,不是说不带这个.sh脚本就不会执行,而是大家一种通用的命名规范而已 2.shell脚本中的第一行一定是:#! /bin/bash.该命令说明,该文件使用的是bas ...

  8. webpack.DefinePlugin

    通过配置了DefinePlugin,那么这里面的标识就相当于全局变量,你的业务代码可以直接使用配置的标识.比如,你通过下面的设置: // webpack.config.js new webpack.D ...

  9. cv_list

    cv_list hmz http://cse.sjtu.edu.cn/OS/OS_files/page0012.htm 邹恒明博士: 美国密歇根大学(University of Michigan-An ...

  10. Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code

    Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...