当前位置:网站首页>[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 .
边栏推荐
- How Charles installs and uses
- CCSP international registered cloud security experts set up examination rooms in China
- 3438. Number system conversion
- SQL learning
- 回编译失败
- 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
- [complete installation package & tutorial] sqlserver basic installation_ Sqlserver completely uninstalled_ Sqlserver custom installation_ Getting started with sqlserver_ SQLSERVER database
- crmeb v4.3部署流程
- 3438. 数制转换
- PHP magic method
猜你喜欢

Idea2020.1.4 packages package collapse

Untitled may

A problem -- about dragging div in JS, when I change div to round, there will be a bug

Introduction to mqtt protocol

RY-D1/1电压继电器

SRTT-110VDC-4H-C时间继电器
Gradle -- package multiple variants with gradle

Instructions for common symbols in kotlin

流畅到让人头皮发麻的单商户商城,你用过吗?

2021-09-02
随机推荐
Untitled may
iPhone苹果手机上一些不想让他人看到的APP应用图标怎么设置手机桌面上的APP应用设置隐藏不让显示在手机桌面隐藏后自己可以正常使用的方法?
Bcompare key expired or bcompare license key revoked
The automatic prompt of vs code code is missing - a tick to solve it
为什么企业需要用户自治的数字身 份
Compose learning notes 1-compose, state, flow, remember
Wonderful frog -- how simple can it be to abandon the float and use the navigation bar set by the elastic box
3559. 围圈报数
Image steganography method
Mlx90640 infrared thermal imager sensor module development notes (VIII)
即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
Specific operation process of network security emergency response
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
day 7/12
The second 1024, come on!
SQL error [1810] [22008]: ora-01810: format code occurs twice
Mlx90640 infrared thermal imager sensor module development notes (VIII)
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
3715. 最少交换次数
Instant experience | further improve application device compatibility with cts-d