当前位置:网站首页>[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;
}
}

边栏推荐
- Server memory failure prediction can actually do this!
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
- LabVIEW loads and uses custom symbols in tree control projects
- [openvx] VX for basic use of objects_ lut
- leetcode刷题:动态规划09(最后一块石头的重量 II)
- Redis basic operation
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- 递归和非递归分别实现求第n个斐波那契数
- WordPress简约mkBlog博客主题模板v2.1
- Golang gets the tag of the loop nested structure
猜你喜欢

沃尔沃:深入人心的“安全感” 究竟靠的是什么?

695. 岛屿的最大面积

AIRIOT答疑第6期|如何使用二次开发引擎?

Weekly recommended short video: how to correctly understand the word "lean"?

Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size

20220727使用汇承科技的蓝牙模块HC-05配对手机进行蓝牙串口的演示

C language to achieve a dynamic version of the address book

单调栈——42. 接雨水——面大厂必须会的困难题

单调栈——739. 每日温度

最新版宝塔安装zip扩展,php -m 不显示的处理方法
随机推荐
Volvo: what on earth does the deep-rooted "sense of security" rely on?
deepstream 检测结果截图
ES6 from getting started to mastering 08: extended object functions
Qt:qmessagebox message box, custom signal and slot
[wrong question]
Do you regret doing automated testing?
Differences among BRD, MRD and PRD
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
[wrong question]mocha and railgun
什么是Tor?Tor浏览器更新有什么用?
Unity backpack system
Tensorboard usage record
做自动化测试,你后悔了吗?
WordPress简约mkBlog博客主题模板v2.1
Qt:QMessageBox消息框、自定义信号和槽
贪心——53. 最大子数组和
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
接口自动化测试,完整入门篇
After 95, Alibaba P7 published the payroll: it's really heartbreaking
input 上传文件并回显 FileReader并限制选择文件时的类型