当前位置:网站首页>【leetcode】34. Find the first and last positions of elements in a sorted array
【leetcode】34. Find the first and last positions of elements in a sorted array
2022-07-02 03:53:00 【Chinese fir sauce_】
subject :
34. Find the first and last positions of the elements in the sort array
Given an array of integers in ascending order nums, And a target value target. Find the start and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
Advanced :
You can design and implement time complexity of O(log n) Does the algorithm solve this problem ?
Example 1:
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Example 2:
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Example 3:
Input :nums = [], target = 0
Output :[-1,-1]
Tips :
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums It is a group of non decreasing numbers
-109 <= target <= 109
Dichotomy :
class Solution {
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
if(n == 0) return new int[]{
-1,-1};
int l = 0, r = n - 1;
int res = 0;
while(l <= r) {
int mid = (l + r) / 2;
if(target > nums[mid]) l = mid + 1;
else if(target < nums[mid]) r = mid - 1;
else {
// Retain the first equal subscript
res = mid;
// Traverse to the left
while(--mid >= 0 && mid<nums.length && nums[mid] == target) continue;
// Traverse to the right
while(++res >= 0 && res < n && nums[res] == target) continue;
return new int[]{
mid + 1, res - 1};
}
}
return new int[]{
-1,-1};
}
}
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
- Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
- 一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
- SQL Yiwen get window function
- Didi open source Delta: AI developers can easily train natural language models
- Raspberry pie GPIO pin controls traffic light and buzzer
- Where can I buy cancer insurance? Which product is better?
- Monkey测试
- 蓝桥杯单片机省赛第八届
- [personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
- Kotlin basic learning 17
猜你喜欢

NLog使用
![[untitled] basic operation of raspberry pie (2)](/img/b4/cac22c1691181c1b09fe9d98963dbf.jpg)
[untitled] basic operation of raspberry pie (2)

The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

First acquaintance with string+ simple usage (II)

蓝桥杯单片机省赛第十一届第一场

Fourier series

初识string+简单用法(二)

JVM knowledge points

The fourth provincial competition of Bluebridge cup single chip microcomputer

"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
随机推荐
What kind of interview is more effective?
NLog use
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
What is the logical structure of database file
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
蓝桥杯单片机省赛第五届
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
Vite: scaffold assembly
Xlwings drawing
Is the product of cancer prevention medical insurance safe?
u本位合约爆仓清算解决方案建议
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
向数据库中存入数组数据,代码出错怎么解决
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
Kotlin basic learning 17
Flutter中深入了解MaterialApp,常用属性解析
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup