当前位置:网站首页>【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)
边栏推荐
- The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
- What do you know about stock selling skills and principles
- 接口调试工具模拟Post上传文件——ApiPost
- SQL:常用的 SQL 命令
- Fingertips life Chapter 4 modules and packages
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- u本位合约爆仓清算解决方案建议
- C language: examples of logical operation and judgment selection structure
- Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
- 蓝桥杯单片机省赛第十一届第二场
猜你喜欢
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
[personal notes] PHP common functions - custom functions
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
[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!
蓝桥杯单片机省赛第十一届
C语言:逻辑运算和判断选择结构例题
Learn more about materialapp and common attribute parsing in fluent
随机推荐
Suggestions on settlement solution of u standard contract position explosion
Xlwings drawing
[mv-3d] - multi view 3D target detection network
Oracle 查看被锁的表和解锁
UI (New ui:: MainWindow) troubleshooting
接口调试工具模拟Post上传文件——ApiPost
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
The 11th Blue Bridge Cup single chip microcomputer provincial competition
SQL:常用的 SQL 命令
蓝桥杯单片机省赛第十届
Blue Bridge Cup SCM digital tube skills
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Where can I buy cancer insurance? Which product is better?
蓝桥杯单片机省赛第六届
go 包的使用
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
Monkey测试
Qt插件之Qt Designer插件实现