当前位置:网站首页>[leetcode] 34. Find the first and last positions of elements in the sorted array
[leetcode] 34. Find the first and last positions of elements in the sorted array
2022-07-28 03:42:00 【Xiaoqu classmate】
34、 Sort the first and last positions of the elements in the array
subject :
Give you an array of integers in non decreasing order nums, And a target value target. Please find out the start position 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].
You must design and implement a time complexity of O(log n) The algorithm to 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]
Tips :
0 <= nums.length <= 10^5
-10 ^9 <= nums[i] <= 10^9
nums It is a group of non decreasing numbers
-10^9 <= target <= 10^9
Their thinking :
The intuitive idea must be to traverse from front to back . Record the first and last encounter with two variables target The subscript , But the time complexity of this method is O(n), Didn't make use of Array ascending Conditions of arrangement .
Two points search
Because the array has been sorted , So the whole array is monotonically increasing , We can use Dichotomy To speed up the search process .
consider target Start and end positions , In fact, what we're looking for is in the array 「 The first is equal to target The location of 」( Write it down as leftIdx) and 「 The first one is greater than target Minus one 」( Write it down as rightIdx).
Binary search , seek leftIdx That is to find the first in the array Greater than or equal to target The subscript , seek rightIdx That is to find the first in the array Greater than target The subscript , Then subtract the subscript by one .
The judgment conditions of the two are different , For code reuse , We define binarySearch(nums, target, lower) It means that nums Binary search in array target The location of , If lower by true, Then find the first Greater than or equal to target The subscript , Otherwise, find the first greater than target The subscript .
Last , because target May not exist in the array , So we need to recheck the two subscripts we get leftIdx and rightIdx, See if they meet the requirements , If the conditions are met, return [leftIdx,rightIdx], Go back if you don't agree [-1,-1][−1,−1].
Reference ideas :
class Solution {
public int[] searchRange(int[] nums, int target) {
int leftIdx = binarySearch(nums, target, true);
int rightIdx = binarySearch(nums, target, false) - 1;
if (leftIdx <= rightIdx && rightIdx < nums.length && nums[leftIdx] == target && nums[rightIdx] == target) {
return new int[]{
leftIdx, rightIdx};
}
return new int[]{
-1, -1};
}
public int binarySearch(int[] nums, int target, boolean lower) {
int left = 0, right = nums.length - 1, ans = nums.length;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > target || (lower && nums[mid] >= target)) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}
return ans;
}
}

边栏推荐
- AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
- CH340 RTS DTR引脚编程驱动OLED
- [错题]Concatenation
- 单调栈——739. 每日温度
- Light year admin background management system template
- The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
- Weekly recommended short video: how to correctly understand the word "lean"?
- 容器相关的概念
- D2DEngine食用教程(4)———绘制文本
- Illustrated: detailed explanation of JVM memory layout
猜你喜欢

20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration

ES6 从入门到精通 # 07:解构赋值

verticle-align行内元素垂直居中对齐

服务器内存故障预测居然可以这样做!

The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor

Mysql基础篇(创建、管理、增删改表)

动态规划——509. 斐波那契数

Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management

leetcode刷题:动态规划08(分割等和子集)

12月份PMP考试首次采用新考纲,该怎么学?
随机推荐
递归和非递归分别实现求第n个斐波那契数
Vertical align align the elements in the row are vertically centered
Swift中的协议
【OPENVX】对象基本使用之vx_convolution
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
MySQL stored procedures use cursors to synchronize data between two tables
高等数学(第七版)同济大学 习题3-6 个人解答
我的创作纪念日
Use of custom annotations
Xctf attack and defense world web master advanced area unserialize3
Redis basic operation
【OPENVX】对象基本使用之vx_matrix
Redis source code analysis (who says C language can't analyze it?)
一个仿win10蓝屏的404页面源码
CH340 RTS DTR引脚编程驱动OLED
【OPENVX】对象基本使用之vx_pyramid
C language to achieve a dynamic version of the address book
【OPENVX】对象基本使用之vx_lut
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
After reading MySQL database advanced practice (SQL xiaoxuzhu)