当前位置:网站首页>【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
2022-07-28 03:35:00 【小曲同学呀】
34、排序数组中查找元素的第一个和最后一个位置
题目:
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
示例1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]
提示:
0 <= nums.length <= 10^5
-10 ^9 <= nums[i] <= 10^9
nums 是一个非递减数组
-10^9 <= target <= 10^9
解题思路:
直观的思路肯定是从前往后遍历一遍。用两个变量记录第一次和最后一次遇见target 的下标,但这个方法的时间复杂度为 O(n),没有利用到数组升序排列的条件。
二分查找
由于数组已经排序,因此整个数组是单调递增的,我们可以利用二分法来加速查找的过程。
考虑 target 开始和结束位置,其实我们要找的就是数组中「第一个等于 target 的位置」(记为 leftIdx)和「第一个大于target 的位置减一」(记为rightIdx)。
二分查找中,寻找 leftIdx 即为在数组中寻找第一个大于等于target 的下标,寻找 rightIdx 即为在数组中寻找第一个大于target 的下标,然后将下标减一。
两者的判断条件不同,为了代码的复用,我们定义 binarySearch(nums, target, lower) 表示在 nums 数组中二分查找target 的位置,如果lower 为true,则查找第一个大于等于target 的下标,否则查找第一个大于target 的下标。
最后,因为target 可能不存在数组中,因此我们需要重新校验我们得到的两个下标leftIdx 和 rightIdx,看是否符合条件,如果符合条件就返回 [leftIdx,rightIdx],不符合就返回 [-1,-1][−1,−1]。
参考思路:
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;
}
}

边栏推荐
- Tensorboard usage record
- Redis基本操作
- TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
- 整合SSM实现增删改查搜索
- 【5G NR】RRC Reject解析
- [wrong question]mocha and railgun
- Redis basic operation
- 【OPENVX】对象基本使用之vx_convolution
- How to arrange PCB screen printing? Please check this manual!
- Win11 how to rename an audio device
猜你喜欢

redis源码分析(谁说C语言就不能分析了?)

Unity背包系统

动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
D2dengine edible tutorial (4) -- draw text

Redis基本操作

Vertical align align the elements in the row are vertically centered

贪心——53. 最大子数组和

Volvo: what on earth does the deep-rooted "sense of security" rely on?

Integrate SSM to realize search of addition, deletion, modification and query

TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
随机推荐
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
Acid characteristics of MySQL transactions and example analysis of concurrency problems
TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
【OPENVX】对象基本使用之vx_lut
D2DEngine食用教程(4)———绘制文本
[openvx] VX for basic use of objects_ lut
"Xiaodeng" network equipment monitoring in operation and maintenance
玩一玩WolframAlpha计算知识引擎
动态规划——62. 不同路径
MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题
Shell: one click deployment PXE
最新版宝塔安装zip扩展,php -m 不显示的处理方法
服务器内存故障预测居然可以这样做!
[wrong question]mocha and railgun
Collection | 0 basic open source data visualization platform flyfish large screen development guide
golang gorm查询任意字段的组装方法
203.移除链表元素
Capacity expansion and reduction of RBD block storage device (VI)
[5g NR] RRC reject analysis
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小