当前位置:网站首页>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;
}
边栏推荐
- Transaction method call @transactional
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- 【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
- 【入门】提取不重复的整数
- [untitled]
- Anddroid 文本合成语音TTS实现
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- [getting started] extract non repeating integers
- Airsim radar camera fusion to generate color point cloud
- Why are some Wills made by husband and wife invalid
猜你喜欢

SharePoint - modify web application authentication using PowerShell
![[redis] it takes you through redis installation and connection at one go](/img/ca/89cb18f0eeb835f021d6a2489681a1.png)
[redis] it takes you through redis installation and connection at one go

【入门】提取不重复的整数

Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field

Set up file server Minio for quick use

Gdip - hatchBrush图案表

軟鍵盤高度報錯

凸印的印刷原理及工艺介绍

Using settoolkit to forge sites to steal user information

【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
随机推荐
php laravel微信支付
PHP laravel wechat payment
Transaction method call @transactional
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
Source code analysis of open source API gateway APIs IX
slice扩容机制分析
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Aardio - Method of self constructed geticonhandle
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
getInputStream() has already been called for this request
Anddroid text to speech TTS implementation
Li Kou daily question - Day 32 -1822 Symbol of array element product
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
[redis] it takes you through redis installation and connection at one go
Aardio - 阴影渐变文字
php laravel微信支付
手工挖XSS漏洞
Scala language learning-07-constructor
How to use layui to display the data in the database in the form of tables