当前位置:网站首页>[binary search] 34 Find the first and last positions of elements in a sorted array
[binary search] 34 Find the first and last positions of elements in a sorted array
2022-07-05 05:16:00 【lee2813】
One 、 subject
Given an array of integers in ascending order nums, And a target value target. Find the start and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
Advanced :
You can design and implement time complexity of O(log n) Does the algorithm solve this problem ?
Example 1:
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Example 2:
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Example 3:
Input :nums = [], target = 0
Output :[-1,-1]
Two 、 Answer key
Solution 1 : Search for ( Time consuming )
Solution 2 : Two points search
Because the array is monotonically increasing , We can use the binary search method to speed up the search process . Here is also set as the left closed right closed interval .
When finding the corresponding element to determine whether it is the position of the first occurrence and the last occurrence of the given value , Use the following strategies :
- Finding the first occurrence position of a given value is equivalent to finding the first greater than ( Because there may be cases without this value ) Subscript equal to the given value
- Finding the last occurrence position of a given value is equivalent to finding the first subscript larger than the given value , subtracting 1
Take finding the location of the first occurrence as an example , The search results are as follows :
- The lookup value does not match the target value ( Less than the target ): Take the left range
- The search value matches the target value ( Greater than equal target value ): Take the right range
- Left boundary > Right border : return
l, Because the matching situation here is greater than or equal to , Moving isl, Therefore, it returnsl
among , The method of using dichotomy to determine the position of elements for comparison is the same , The comparison strategy is different .
3、 ... and 、 Code
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.size()==0) return vector<int>{
-1,-1};
int leftindex = fun1(nums,target);
int rightindex = fun2(nums,target);
if(leftindex==nums.size() || nums[leftindex] != target) return vector<int>{
-1,-1};
return vector<int>{
leftindex,rightindex};
}
int fun1(vector<int>& nums,int target){
int l = 0,r = nums.size() - 1;
int mid;
while(l<=r){
mid = (l + r) / 2;
if(nums[mid] >= target) r = mid - 1;
else l = mid + 1;
}
return l;
}
int fun2(vector<int>& nums,int target){
int l = 0,r = nums.size() - 1;
int mid;
while(l<=r){
mid = (l + r) / 2;
if(nums[mid] > target) r = mid - 1;
else l = mid + 1;
}
return l-1;
}
};
边栏推荐
猜你喜欢

National teacher qualification examination in the first half of 2022

UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存

C4D simple cloth (version above R21)

远程升级怕截胡?详解FOTA安全升级
![[turn to] MySQL operation practice (III): table connection](/img/70/20bf9b379ce58761bae9955982a158.png)
[turn to] MySQL operation practice (III): table connection
![[trans]: spécification osgi](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[trans]: spécification osgi

2022/7/2做题总结

服务熔断 Hystrix
![[turn]: OSGi specification in simple terms](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[turn]: OSGi specification in simple terms
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出
随机推荐
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
嵌入式数据库开发编程(六)——C API
This article is good
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[turn]: Apache Felix framework configuration properties
Research on the value of background repeat of background tiling
Panel panel of UI
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
Sixth note
用 Jmeter 工具做个小型压力测试
Unity intelligent NPC production -- pre judgment walking (method 1)
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
[转]MySQL操作实战(三):表联结
2022年上半年国家教师资格证考试
3dsmax scanning function point connection drawing connection line
cocos2dx_ Lua particle system
Unity get component
服务熔断 Hystrix
嵌入式数据库开发编程(零)
PMP candidates, please check the precautions for PMP examination in July