当前位置:网站首页>Leetcode search insert location
Leetcode search insert location
2022-06-30 05:47:00 【accumulate steadily ض】
Power button 35. Search insert location

The title requires you to find a number , And if the target element doesn't return where it should be inserted in the array , The title prompt array is an ascending array without repeating elements , obviously , This just meets the requirements of binary search , It's easy to find elements by setting a binary template , But what about inserting ?,
for instance
For example, we are going to be in 1 3 5 6 Find number 2, And insert position , We can use binary search
We can find that the intermediate element is 3, But keep looking 2,
![]()
We don't have 2, but 2 The position to be inserted is 3 The location of , That is to say, we will 2 Insert into 3 And the subscript is 1 Location , However, if you perform the previous step, you will never find the location to insert , So we just need to find the first element that is greater than or equal to the target element , If the intermediate element is exactly equal to the target element , Then return the target element subscript , If it is larger than the target element , This position is where the target element is to be inserted , But there's one more thing , If it is not found in the array and there is no position in the array to insert , It must be larger than all the elements in the array , Then insert it after the last element , That is, the subscript is the array size , So we can define a ans Is the size of the array , If not found in the array , We just insert it to the next position of the last element in the array .
int searchInsert(int* nums, int numsSize, int target){
int left =0;
int right = numsSize-1;
int ans =numsSize;//(1)
while(left<=right){//(2)
int mid =(right-left)/2+left;
if(nums[mid]>=target){//(3)
ans =mid;
right =mid-1;
}
else{
left=mid+1;
}
}
return ans;//(4)
}1): To find if the target element is not in the array and is larger than the element in the array , It must be inserted where the subscript is the size of the array
2): Adopt left closing and right closing
3): If the intermediate element is exactly equal to the target element , Then return the target element subscript , If it is larger than the target element , This position is where the target element is to be inserted
4): If it is not found in the array and there is no position in the array to insert , It must be larger than all the elements in the array , Then insert it after the last element , That is, the subscript is the array size
边栏推荐
- [Motrix] download Baidu cloud files using Motrix
- 1380. lucky numbers in matrices
- Set of XXL job principles
- 抓取手机端变体组合思路设想
- Shopping list--
- 旋转框目标检测mmrotate v0.3.1入门
- Word frequency statistics (string, list)
- Answer sheet for online assignment of "motor and drive" of Xijiao 21 autumn (IV) [standard answer]
- Detailed explanation of the loss module of mmdet
- Video summary of my station B
猜你喜欢

Create priority queue

Introduction to mmcv common APIs

Solidity - 安全 - 重入攻击(Reentrancy)

Transfer the token on the matic-erc20 network to the matic polygon

1380. lucky numbers in matrices

inno setup 最简单的自定义界面效果

English grammar_ Adjective / adverb Level 3 - superlative

Why can transformer break into the CV world and kill CNN?

Delete the repeating elements in the sorting list (simple questions)

Rotating box target detection mmrotate v0.3.1 getting started
随机推荐
旋转框目标检测mmrotate v0.3.1 学习配置
Transfer the token on the matic-erc20 network to the matic polygon
token 过期后,如何自动续期?
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
Leader: who can use redis expired monitoring to close orders and get out of here!
超简单 STM32 RTC闹钟 时钟配置
1380. lucky numbers in matrices
[Blue Bridge Road -- bug free code] DS1302 time module code analysis
Expansion method of unity scanning circle
How to write a thesis
Is it safe to open an account and trade with a compass?
Sword finger offer 22 The penultimate node in the linked list
Solidy - fallback function - 2 trigger execution modes
Dao -- a beautiful new world?
Solidity - Security - reentrancy attack
Visualization of 3D geological model based on borehole data by map flapping software
Do you know how to show the health code in only 2 steps
Wechat applet training 2
Sound net, debout dans le "sol" de l'IOT
旋转框目标检测mmrotate v0.3.1入门
