【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 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- h5 打造全屏体验 element.requestFullscreen()
google打造全屏体验 https://developers.google.cn/web/fundamentals/native-hardware/fullscreen/ 以前github上的 ht ...
- codeforces#525 Div2---ABC
A---Ehab and another constriction problem https://codeforc.es/contest/1088/problem/A 题意:给定一个数$x$找两个在 ...
- PL-SLAM
双目 1.PL-SLAM: a Stereo SLAM System through the Combination of Points and Line Segments ubuntu14.04配置 ...
- CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]
题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条 ...
- HDU 5985/nowcoder 207D - Lucky Coins - [概率题]
题目链接:https://www.nowcoder.com/acm/contest/207/D 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985 ...
- CMSampleBufferRef转换
参考链接:https://blog.csdn.net/shenyi0106/article/details/47004039 https://blog.csdn.net/jeffasd/article ...
- day2_抓包-抓包工具Charles
1.Charles功能简单描述 1)定位问题,前端的.后端的问题 2)发出去的请求,请求头.请求体,返回的数据 3)拦截请求,修改请求 2.Charles抓包(Android手机) 1.要求手机得和你 ...
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...
- linux read()和write
参考http://www.cnblogs.com/xiehongfeng100/p/4619451.html 1. read总是在接收缓冲区有数据时立即返回,而不是等到给定的read buffer填满 ...
- 遍历出文档内所有元素的tagName
//深度优先 function deepLogTagNames(parentNode){ console.log(parentNode.tagName); const childNodes=paren ...