当前位置:网站首页>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
边栏推荐
- Did you know that WPS can turn on eye protection mode?
- What are membrane stress and membrane strain
- Bev instance prediction based on monocular camera (iccv 2021)
- Learning automation ppt
- OSPF - authentication and load balancing summary (including configuration commands)
- The fourth day of learning C language for Asian people
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- The minecraft server address cannot be refreshed.
- Assembly learning tutorial: accessing memory (3)
- Video summary of my station B
猜你喜欢

After getting these performance test decomposition operations, your test path will be more smooth

Introduction to mmcv common APIs

Xctf--Web--Challenge--area Wp

旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)

Idea of capturing mobile terminal variant combination

Xctf attack and defense world crypto advanced area

Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)

86. separate linked list

Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
随机推荐
Codeforces C. Andrew and Stones
Did you know that WPS can turn on eye protection mode?
How to prevent source code leakage in enterprises and institutions
剑指 Offer 18. 删除链表的节点
What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
炒股用指南针开户交易安全吗?
/Path/to/ idiom, not a command
PKCs 12:personal information exchange syntax v1.1 translation part I
Database SQL language 06 single line function
【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
Word frequency statistics (string, list)
Here comes the nearest chance to Ali
Visualization of 3D geological model based on borehole data by map flapping software
Solidy - fallback function - 2 trigger execution modes
[untitled] user defined function
Create priority queue
86. 分隔链表
The minecraft server address cannot be refreshed.
超简单 STM32 RTC闹钟 时钟配置
Promise knowledge points
