当前位置:网站首页>[notes for question brushing] search the insertion position (flexible use of dichotomy)
[notes for question brushing] search the insertion position (flexible use of dichotomy)
2022-07-25 07:14:00 【Qihai】
subject :
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Example 2:Input : nums = [1,3,5,6], target = 2
Output : 1
Example 3:Input : nums = [1,3,5,6], target = 7
Output : 4
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums by No repeating elements Of Ascending Arrange arrays
-104 <= target <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/search-insert-position
First , It is clearly required that the use time complexity is O(log n) The algorithm of , So I choose dichotomy here .( Because it's in order )
solution :
First, clarify how the dichotomy is filtered in the ordered array : Find the number in the middle first , Found the subscript that returns the middle . Otherwise, if it is larger than the middle number , Then the interval changes to the right to find , When I was young, I went to the left section to find the middle . Know that the interval is shortened to 0 perhaps 1 until .
But this problem can't be found only by using the above ordinary dichotomy . Because there is another requirement to return the subscript if it cannot be found .
therefore , We need an uncertain state . Exclude the array on the premise that it has several numbers , Then this number should be sorted in the subscript of the larger number , It should be smaller than it before .( If target Larger than all the numbers of the full array should be regarded as a special case ) that , Until the end of the cycle , This endpoint is the subscript we are looking for .
The above means :targe If you compare mid Large number , that , At this time [begin, end] There must be no match for its insertion position , conversely , If targe Than mid The number is less than or equal to , that [begin, end] There is a position that matches its insertion , That's right. end. therefore , The code is as follows :
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;
}
边栏推荐
- 2022 Shenzhen cup
- [OBS] DTS sent by video packet_ USEC calculation
- Kubernates-1.24.2 (latest version) + containerd + nexus
- [Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators
- 从ACL 2022 Onsite经历看NLP热点
- CTF Crypto---RSA KCS1_ Oaep mode
- MySQL remote login
- Yolov7 model reasoning and training its own data set
- Mathematics Olympiad vs Informatics Olympiad (July 19, 2022)
- With apple not making money, the 2trillion "fruit chain" abandons "fruit" and embraces "special"
猜你喜欢

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

Devops has been practiced for many years. What is the most painful thing?

2022 Shenzhen cup
![[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x](/img/7e/1d27e3f1856ab8c6cbfc5221c717bb.png)
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x

从ACL 2022 Onsite经历看NLP热点

vulnhub CyberSploit: 1
![[daily question 1] 1184. Distance between bus stops](/img/36/2bbb8cc2a1fdd08070a5df7527e692.png)
[daily question 1] 1184. Distance between bus stops

BOM overview

2022天工杯CTF---crypto1 wp

线代(矩阵‘)
随机推荐
Wechat applet switchtab transmit parameters and receive parameters
论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION
Microorganisms are healthy. Don't exclude microorganisms in the human body
"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary problem solution (incomplete)
集群聊天服务器:项目问题汇总
2022 Tiangong cup ctf--- crypto1 WP
Yolov7 model reasoning and training its own data set
【obs】视频包发送的dts_usec 计算
Leetcode sword finger offer brush question notes
微信小程序request请求携带cookie,验证是否已登录
Vscode saves setting configuration parameters to the difference between users and workspaces
Dynamic memory management
Argocd user management, RBAC control, script login, APP synchronization
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
Oracle table creation statement template
Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
JS data type judgment - Case 6 delicate and elegant judgment of data type
How to learn C language?
第一启富金怎么样
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C