当前位置:网站首页>[leetcode] 35. Search the insertion position
[leetcode] 35. Search the insertion position
2022-07-28 15:16:00 【Xiaoqu classmate】
35、 Search insert location
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 <= 10^4
-10^4 <= nums[i] <= 10^4
nums by No repeating elements Of Ascending Arrange arrays
-10^4 <= target <= 10^4
Their thinking :
The premise of this topic is that the array is an ordered array , This is also the basic condition of using binary search .
After that, everyone Just see that the array given in the question is Ordered array , Can think about whether it can be used Dichotomy .
At the same time, the title also emphasizes that in the array No repeating elements , Because once there's a repeating element , The following table of elements returned by binary search may not be unique .
Deal with the following four situations respectively :
// The target value precedes all elements of the array [0, -1]
// The target value is equal to an element in the array return mid;
// The position where the target value is inserted into the array [left, right],return right + 1
// Where the target value is after all elements of the array [left, right], This is a right closed interval , therefore return left
Reference code :
class Solution {
public int searchInsert(int[] nums, int target) {
int right = nums.length-1;
int left = 0;
while(left<=right){
int mid = (left+right)/2;
if(nums[mid]<target){
left = mid+1;
}else if(nums[mid]>target){
right = mid-1;
}else{
return mid;
}
}
return left;
}
}
summary
I hope that through this topic , You will find that you usually write dichotomy , Why can't you write well , Because the definition of interval is not clear .
Determine whether the interval to be searched is left closed and right open [left, right), Still left closed and closed [left, right], This is the invariant .
And then in In the loop of binary search , Adhere to the principle of loop invariants , A lot of details , Naturally, you will know how to deal with .
边栏推荐
- 3559. Ring counting
- Machine learning related concepts
- Three pop-up boxes commonly used in JS
- 网络安全应急响应具体操作流程
- DAY:7/11
- Buuctf partial solution
- 7/13(水塘抽样)
- MLX90640 红外热成像仪传感器模块开发笔记(八)
- The second 1024, come on!
- How to set some app application icons on the iPhone Apple phone that you don't want others to see? How to hide the app application settings on the mobile desktop so that they can be used normally afte
猜你喜欢

How to set some app application icons on the iPhone Apple phone that you don't want others to see? How to hide the app application settings on the mobile desktop so that they can be used normally afte

使用cpolar发布树莓派网页(apache2的安装测试)

crmeb 标准版Window+phpstudy8安装教程(一)

Compilation language and interpretation language

DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决

7/13(水塘抽样)

day 7/12

JWY-32B电压继电器

DAY:7/11

模板注入总结
随机推荐
Development status of security and privacy computing in China
JWY-32B电压继电器
The modified network card name of rocky foundation is eth0
Compose learning notes 2 - launchedeffect, status and status management
软件开发三大痛点!小程序容器如何解决?
云计算需要考虑的安全技术列举
经典Dijkstra与最长路
No files or folders found to process
2021 year end summary of gains and losses
crmeb标准版4.4都会增加哪些功能
3564. 日期类
SQL error [1810] [22008]: ora-01810: format code occurs twice
Three pain points of software development! How to solve the applet container?
Instant experience | further improve application device compatibility with cts-d
Specific operation process of network security emergency response
JS学习笔记24-28:结束
从thinkphp远程代码执行学php反射类
MySQL authorization method
[complete installation package & tutorial] sqlserver basic installation_ Sqlserver completely uninstalled_ Sqlserver custom installation_ Getting started with sqlserver_ SQLSERVER database
汇编学习