【LeetCode每天一题】Search Insert Position(搜索查找位置)
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(搜索查找位置)的更多相关文章
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [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 ...
- 【算法】LeetCode算法题-Search Insert Position
这是悦乐书的第152次更新,第154篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第11题(顺位题号是35).给定排序数组和目标值,如果找到目标,则返回索引. 如果没有, ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- lintcode:Search Insert Position 搜索插入位置
题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- RabbitMQ 学习
参考:https://www.rabbitmq.com/getstarted.html 先在本地安装RabbitMQ 组件(需要安装Erlang组件),启动服务. 激活 RabbitMQ's Mana ...
- sshpass 配置密码登录ssh
tar -zxvf sshpass-1.06.tar.gzcd sshpass-1.06./configuremake && make install sshpass -p userp ...
- thinkPHP框架 简单的删除和修改数据的做法 和 模板继承的意思大概做法
BiaodanController.class.php控制器页面 <?php namespace Admin\Controller; use think\Controller; class Bi ...
- SQL Server2012远程访问设置
http://jingyan.baidu.com/article/a681b0de3bdb7b3b19434667.html?qq-pf-to=pcqq.group 1.打开SQL server201 ...
- AD 16 下绘图的几个技巧
1.绘制封装如果引脚过多怎么办,使用阵列粘贴功能 首先建立一个焊盘,然后选中,使用 ctrl + c 复制,注意复制确认的时候,鼠标一定要点击到焊盘中间. 选择阵列粘贴 条款就是你要复制多少个,增量就 ...
- pycharm 下的djiango使用
创建工程可以在虚拟环境下运行,创建工程后使用命令 在python 下的命令窗口(Terminal) python3 manage.py startapp django_web (或者 python3替 ...
- win10环境下搭建zookeeper伪集群
一.下载zookeeper https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 这里笔者下载的是zookeeper-3.3.6 二.配置zoo ...
- ==、===和Object.is()的区别
==.===和Object.is()的区别 一. 定义: ==:等同,比较运算符,两边值类型不同的时候,先进行类型转换,再比较: ===:恒等,严格比较运算符,不做类型转换,类型不同就是不等: Obj ...
- 关于richtextbox改变字体颜色,加下划线
参考了三份有用的资料: 1.关于richtextbox设置字体颜色的问题 http://biancheng.dnbcw.net/c/180381.html 2.C#Winform使用扩展方法自定义富文 ...
- 用github搭建网站
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...