当前位置:网站首页>【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)
- 一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
- Oracle viewing locked tables and unlocking
- Class design basis and advanced
- 0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
- NLog使用
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- 傅里叶级数
- Oracle 常用SQL
- 数据库文件逻辑结构形式指的是什么
猜你喜欢
![[yolo3d]: real time detection of end-to-end 3D point cloud input](/img/5e/f17960d302f663db75ad82ae0fd70f.jpg)
[yolo3d]: real time detection of end-to-end 3D point cloud input

Unity脚本的基础语法(6)-特定文件夹
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

Account management of MySQL

The first practical project of software tester: web side (video tutorial + document + use case library)

SQL Yiwen get window function

Influence of air resistance on the trajectory of table tennis
![[untitled] basic operation of raspberry pie (2)](/img/b4/cac22c1691181c1b09fe9d98963dbf.jpg)
[untitled] basic operation of raspberry pie (2)
![[designmode] builder model](/img/e8/855934d57eb6868a4d188b2bb1d188.png)
[designmode] builder model

QT designer plug-in implementation of QT plug-in
随机推荐
JS generate random numbers
Network connection mode of QT
MySQL index, transaction and storage engine
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
SQL Yiwen get window function
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
JVM knowledge points
regular expression
Unity脚本的基础语法(7)-成员变量和实例化
Interface debugging tool simulates post upload file - apipost
蓝桥杯单片机省赛第十二届第二场
初识string+简单用法(二)
蓝桥杯单片机省赛第十二届第一场
Oracle viewing locked tables and unlocking
The 8th Blue Bridge Cup single chip microcomputer provincial competition
QT designer plug-in implementation of QT plug-in
【IBDFE】基于IBDFE的频域均衡matlab仿真
Visual slam Lecture 3 -- Lie groups and Lie Algebras