当前位置:网站首页>【刷题笔记】搜索插入位置(二分法的活用)
【刷题笔记】搜索插入位置(二分法的活用)
2022-07-25 07:08:00 【柒海啦】
题目:
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。
示例 1:
输入: nums = [1,3,5,6], target = 5
输出: 2
示例 2:输入: nums = [1,3,5,6], target = 2
输出: 1
示例 3:输入: nums = [1,3,5,6], target = 7
输出: 4
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums 为 无重复元素 的 升序 排列数组
-104 <= target <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/search-insert-position
首先,明确要求使用时间复杂度为O(log n)的算法,所以我这里选择二分法。(因为是排好序了的)
解法:
首先明确二分法在有顺序的数组里面是如何进行筛选的:先找中间的那个数,找到了返回中间的下标。否则如果比中间的数要大,那么区间就变到了右边去找,小就去左边区间取中去找。知道区间缩短为0或者1为止。
但是此题如果只是用上面的普通二分查找是不行的。因为此时还有一个如果找不到就需要返回下标的要求。
所以,我们就需要一个不确定的状态。在将该数组有数的前提下排除在外,那么此数应该就是在比其大的那个数的下标进行排序,往前就应该比其小了。(如果target比全数组所有数大应该当做特殊情况)那么,直到循环结束,此端点也就是我们要找的那个下标。
上述的意思也就是:targe如果比mid所在数大,那么,此时的[begin, end]一定没有符合其插入的位置,反之,如果targe比mid所在数小于或者等于,那么[begin, end]就存在着符合其插入的位置,正好就是end。所以,代码如下:
int searchInsert(int* nums, int numsSize, int target) {
if (nums[numsSize - 1] < target)
return numsSize;
int begin = 0;
int end = numsSize - 1;
while (begin < end)
{
int middle = begin + (end - begin) / 2;
if (nums[middle] < target)
{
begin = middle + 1;
}
else
{
end = middle;
}
}
return end;
}
边栏推荐
- 100 GIS practical application cases (seventeen) - making 3D map based on DEM
- Builder pattern
- Rust标准库-实现一个TCP服务、Rust使用套接字
- Health clock in daily reminder tired? Then let automation help you -- hiflow, application connection automation assistant
- 解密NumPy求解梯度的一个关键难点
- Mathematics Olympiad vs Informatics Olympiad (July 19, 2022)
- Devops has been practiced for many years. What is the most painful thing?
- QT actual combat case (53) -- using qdrag to realize the drag puzzle function
- 微信小程序request请求携带cookie,验证是否已登录
- Du Jiao sieve
猜你喜欢

2022深圳杯

10 minutes to understand how JMeter plays with redis database

YOLOv7模型推理和训练自己的数据集

vulnhub CyberSploit: 1

微信小程序switchTab传参以及接收参数

Qt实战案例(53)——利用QDrag实现拖拽拼图功能

Boiling short drama Jianghu: nine of the ten production teams are shooting, with a head sharing fund of more than 30million, and users are addicted to paying routines

Insight into mobile application operation growth in 2022 white paper: the way to "break the situation" in the era of diminishing traffic dividends

论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION

Wechat applet switchtab transmit parameters and receive parameters
随机推荐
Over adapter mode
Yolov7 model reasoning and training its own data set
Rambus announces ddr5 memory interface chip portfolio for data centers and PCs
2022 Shenzhen cup
Cointelegraph撰文:依托最大的DAO USDD成为最可靠的稳定币
runtimecompiler 和 runtimeonly是什么
A scene application of 2D animation
Precautions for starting up the server of Dahua Westward Journey
共模电感听过很多次,但是什么原理你们真的懂吗?
Labelme labels different objects, displays different colors and batch conversion
Install, configure, and use the metroframework in the C WinForms application
Ant design input search box listens for allowclear event separately
Leetcode sword finger offer brush question notes
A little consideration of strategic mode
杜教筛
CTF Crypto---RSA KCS1_ Oaep mode
【电脑讲解】去电脑维修店修电脑需要注意什么?
Shell run command
[yolov5 practice 3] traffic sign recognition system based on yolov5 - model training
Example demonstration of math.random() random function