当前位置:网站首页>Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置
Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置
2022-07-01 08:08:00 【范谦之】
题目描述
给你一个按照非递减顺序排列的整数数组 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 ) O(\log n) O(logn) 的复杂度,要使用二分查找。
可以找到(target-0.5)的位置,代表target的开始位置;
通过找到(target+0.5)的位置,找到target的结束位置。
但是,要注意边界情况。例如:
nums = [5,7,7,8,8,10], target = 8
如果要找到7.5的下标 i n d l ind_l indl,那么得到下标3;如果要找到8.5的下标 i n d r ind_r indr,那么得到下标5,所以要加上一下操作:
如果 n u m s [ i n d l ] < t a r g e t nums[ind_l]<target nums[indl]<target, 那么 i n d l + + ind_l++ indl++
如果 n u m s [ i n d l ] > t a r g e t nums[ind_l]>target nums[indl]>target, 那么 i n d l − − ind_l-- indl−−
代码
int findInd(int[] nums, double target) {
int l = 0, r = nums.length-1;
while(l < r) {
int mid = (l+r) / 2;
int t = nums[mid];
if(t == target) {
return mid;
} else if (t < target){
l = mid+1;
} else {
r = mid-1;
}
}
return l;
}
public int[] searchRange(int[] nums, int target) {
if(nums.length == 0) {
int[] t = {
-1, -1};
return t;
}
int[] res = new int[2];
res[0] = findInd(nums, target-0.5);
res[1] = findInd(nums, target+0.5);
if(nums[res[0]] < target) res[0]+=1;
if(nums[res[1]] > target) res[1]--;
if(res[0] > res[1]) {
res[0] = -1;
res[1] = -1;
}
return res;
}
边栏推荐
猜你喜欢

When using charts to display data, the time field in the database is repeated. How to display the value at this time?

Aardio - Shadow Gradient Text

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?

STM32 uses esp01s to go to the cloud, mqtt FX debugging

Utiliser Beef pour détourner le navigateur utilisateur
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]

On several key issues of digital transformation

软键盘高度报错

SharePoint - modify web application authentication using PowerShell
随机推荐
Airsim雷达相机融合生成彩色点云
[introduction] approximate value
How can beginners correctly understand Google's official suggested architectural principles (questions?)
Koltin35, headline Android interview algorithm
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
【Redis】一气呵成,带你了解Redis安装与连接
AArdio - 【问题】bass库回调时内存增长的问题
栈实现计算器
Teach you how to apply for domestic trademark online step by step
How to prevent the other party from saying that he has no money after winning the lawsuit?
【力扣10天SQL入门】Day10 控制流
Scala语言学习-07-构造器
EDA开源仿真工具verilator入门6:调试实例
slice扩容机制分析
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
STM32 uses esp01s to go to the cloud, mqtt FX debugging
Li Kou daily question - Day 32 -1822 Symbol of array element product
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Gru of RNN
Provincial election + noi Part VI skills and ideas