当前位置:网站首页>【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)
边栏推荐
- 5G時代全面到來,淺談移動通信的前世今生
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- The 7th Blue Bridge Cup single chip microcomputer provincial competition
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- Blue Bridge Cup single chip microcomputer sixth temperature recorder
- Qt插件之Qt Designer插件实现
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- Suggestions on settlement solution of u standard contract position explosion
- Unity脚本的基础语法(6)-特定文件夹
猜你喜欢
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
初识string+简单用法(二)
《动手学深度学习》(二)-- 多层感知机
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
[designmode] Prototype Pattern
What do you know about stock selling skills and principles
接口调试工具模拟Post上传文件——ApiPost
蓝桥杯单片机省赛第十一届第一场
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
随机推荐
Vite: configure IP access
蓝桥杯单片机省赛第八届
Kotlin basic learning 13
向数据库中存入数组数据,代码出错怎么解决
[yolo3d]: real time detection of end-to-end 3D point cloud input
The 10th Blue Bridge Cup single chip microcomputer provincial competition
BiShe cinema ticket purchasing system based on SSM
蓝桥杯单片机省赛第五届
毕设-基于SSM电影院购票系统
[punch in] flip the string (simple)
傅里叶级数
Class design basis and advanced
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
蓝桥杯单片机省赛第十二届第二场
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
蓝桥杯单片机第六届温度记录器
Oracle 常用SQL
[mv-3d] - multi view 3D target detection network
蓝桥杯单片机省赛第十二届第一场