当前位置:网站首页>34. 在排序数组中查找元素的第一个和最后一个位置-二分查找、双指针
34. 在排序数组中查找元素的第一个和最后一个位置-二分查找、双指针
2022-06-10 22:12:00 【hequnwang10】
一、题目描述
给定一个按照升序排列的整数数组 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]
二、解题
二分查找
这题进阶需要O(log(n))的时间复杂度,所以就想到二分查找,如果在一个升序的不重复的数组中找一个目标数字比较好写,这里的数组的数字是重复的,所以可以分为找目标值在数组中第一次出现的位置,然后在找第二次出现的位置,然而第二次出现的位置可以转为目标值+1在数组中出现第一次的位置,然后该位置在-1。
class Solution {
public int[] searchRange(int[] nums, int target) {
//排序的数组 log(n) 二分查找
if(nums == null || nums.length == 0){
return new int[]{
-1,-1};
}
int start = binsearch(nums,target);
int end = binsearch(nums,target+1)-1;
//对start 和 end 进行边界判断
if(start == nums.length || nums[start] != target){
return new int[]{
-1,-1};
}else{
return new int[]{
start,end};
}
}
//二分查找
public int binsearch(int[] nums,int target){
int left = 0,right = nums.length;
while(left < right){
int mid = left + (right - left) / 2;
//如果中值小于目标值 往右边查找
if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] == target){
//目标值等于中值 在左侧查找
right = mid;
}else{
//往左边找
right = mid;
}
}
return left;
}
}
左右遍历
双指针遍历,定义两个指针分别从左往右和从右往左遍历,如果找到第一次出现的数值,就记录该下标。
class Solution {
public int[] searchRange(int[] nums, int target) {
//左右遍历
int[] res = new int[]{
-1,-1};
int left = 0,right = nums.length - 1;
if(nums == null || nums.length == 0){
return res;
}
//从左往右遍历
for(int i = left;i<=right;i++){
if(nums[i] == target){
res[0] = i;
break;
}
}
//从右往左遍历
for(int i = right;i>=left;i--){
if(nums[i] == target){
res[1] = i;
break;
}
}
return res;
}
}

边栏推荐
- Flowable BPMN相关知识
- ICML2022 | 從零開始重新審視端到端的語音到文本翻譯
- Kotlin语言现在怎么不火了?
- Static routing configuration of serial interface in router experiment (Supplement)
- 完美解码PureCodec 20220601
- PwnTheBox,Web:hello
- Development and implementation of AI intelligent video analysis easycvr platform device channel batch deletion function
- Software features and functions of the blind box mall app system development
- "Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry
- Simulated 100 questions and simulated examination of G2 utility boiler stoker examination in 2022
猜你喜欢

数组、List、Set、Map、Properties依赖注入格式

PwnTheBox,Pwn:tutorial1

简单阻抗匹配电路及公式

2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation

原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布

样板小作坊

SQL查询优化原理实例分析

【原创】医鹿APP九价HPV数据抓包分析

Software features and functions of the blind box mall app system development

laravel8 实现阿里云文件上传
随机推荐
开中银证券账户安全吗?风险高吗?
DC4 of vulnhub
PwnTheBox,Web:hello
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
"Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry
Question bank and simulation test of 2022 tea artist (intermediate) operation certificate examination
数据与信息资源共享平台(五)
IP反查域名
LeetCode+ 16 - 20
Sentinel
Design language testing for functional testing: what tests are included in functional testing? What is the role of each
[gmm+kde] target tracking simulation based on GMM and KDE kernel estimation of MATLAB
【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
Vscode common shortcuts
AI智能视频分析EasyCVR平台设备通道批量删除功能的开发实现
分布式基础
Project training 10 - backup of specific databases
IP anti query domain name
字蛛(font-spider)教学——ttf/otf字体文件压缩
中金财富证券证券股票开户安全吗?靠谱吗?