当前位置:网站首页>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
边栏推荐
- Set of XXL job principles
- 旋转框目标检测mmrotate v0.3.1 学习配置
- I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
- [road of system analyst] collection of wrong topics in Project Management Chapter
- Visualization of 3D geological model based on borehole data by map flapping software
- 2022年,谁在推动音视频产业的新拐点?
- 剑指 Offer 18. 删除链表的节点
- Redistemplate common method summary
- OSPF - authentication and load balancing summary (including configuration commands)
- 超简单 STM32 RTC闹钟 时钟配置
猜你喜欢

OSPF - authentication and load balancing summary (including configuration commands)

Do you know how to show the health code in only 2 steps

Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation

如何制作CSR(Certificate Signing Request)文件?

Vfpbs uploads excel and saves MSSQL to the database

动态规划--怪盗基德的滑翔翼

At the beginning of 2022, people who are ready to change jobs should pay attention to

Shenzhou ares tx6 boot logo modification tutorial

Use of OpenCL thread algebra library viennacl

Redistemplate common method summary
随机推荐
Database SQL language 06 single line function
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
Baiwen.com 7 days Internet of things smart home learning experience punch in the third day
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
Xctf attack and defense world crypto advanced area
Force deduction exercise -- deleting repeated items in ordered sequence 1.0
3D rotation album
声网,站在物联网的“土壤”里
炒股用指南针开户交易安全吗?
The minecraft server address cannot be refreshed.
English语法_形容词/副词3级-最高级
Dao -- a beautiful new world?
Qt之QListView的简单使用(含源码+注释)
Solidy - fallback function - 2 trigger execution modes
Access is denied encountered when vfpbs calls excel under IIS
Expansion method of unity scanning circle
Sword finger offer 22 The penultimate node in the linked list
Using lazy < t > in C # to realize singleton mode in WPF
Xctf--Web--Challenge--area Wp
Word frequency statistics (string, list)
