当前位置:网站首页>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;
}
边栏推荐
- 【入门】提取不重复的整数
- 【力扣10天SQL入门】Day9 控制流
- The Windows C disk is full
- 程序员养生宝典
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
- 初学者如何正确理解google官方建议架构原则(疑问?)
- OJ输入输出练习
- 2022.6.30 省赛+蓝桥国赛记录
- Soft keyboard height error
- Rumtime 1200 upgrade: London upgrade support, pledge function update and more
猜你喜欢

How outlook puts together messages with the same discussion

Set up file server Minio for quick use

Latex formula code

SQL number injection and character injection

Hijacking a user's browser with beef

軟鍵盤高度報錯

Cyclic neural network

Airsim radar camera fusion to generate color point cloud

Access报表实现小计功能

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
随机推荐
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
Koltin35, headline Android interview algorithm
How can beginners correctly understand Google's official suggested architectural principles (questions?)
[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
Android screen adaptation (using constraintlayout), kotlin array sorting
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
Set up file server Minio for quick use
Adding color blocks to Seaborn clustermap matrix
源代码加密的意义和措施
Insufficient executors to build thread pool
Book of quantitative trading - reading notes of the man who conquers the market
Cyclic neural network
seaborn clustermap矩阵添加颜色块
Lm08 mesh series mesh inversion (fine)
【入门】取近似值
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
程序员养生宝典
AArdio - 【问题】bass库回调时内存增长的问题