当前位置:网站首页>Leetcode t34: find the first and last positions of elements in a sorted array
Leetcode t34: find the first and last positions of elements in a sorted array
2022-07-01 08:18:00 【Fan Qianzhi】
Title Description
Give you an array of integers in non decreasing order nums, And a target value target. Please find out the start position 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].
You must design and implement a time complexity of O(log n) The algorithm to 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]
Ideas
To meet the O ( log n ) O(\log n) O(logn) Complexity , To use binary search .
Can find (target-0.5) The location of , representative target The beginning of ;
By finding (target+0.5) The location of , find target At the end of .
however , Pay attention to the boundary situation . for example :
nums = [5,7,7,8,8,10], target = 8
If you want to find 7.5 The subscript i n d l ind_l indl, Then get the subscript 3; If you want to find 8.5 The subscript i n d r ind_r indr, Then get the subscript 5, So we need to add some operations :
If n u m s [ i n d l ] < t a r g e t nums[ind_l]<target nums[indl]<target, that i n d l + + ind_l++ indl++
If n u m s [ i n d l ] > t a r g e t nums[ind_l]>target nums[indl]>target, that i n d l − − ind_l-- indl−−
Code
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;
}
边栏推荐
- Aardio - 阴影渐变文字
- window c盘满了
- shardingSphere
- Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置
- [untitled]
- Using settoolkit to forge sites to steal user information
- XX attack - reflective XSS attack hijacking user browser
- ContentType所有类型对比
- CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
猜你喜欢

【入门】取近似值

Office365 - how to use stream app to watch offline files at any time

5大组合拳,解决校园6大难题,护航教育信息化建设

How to check ad user information?

Comprehensive experiment Li

SharePoint - modify web application authentication using PowerShell

How outlook puts together messages with the same discussion

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

谈谈数字化转型的几个关键问题
随机推荐
7-26 word length (input and output in the loop)
String coordinates of number to excel
Cmake I two ways to compile source files
On several key issues of digital transformation
使用beef劫持用户浏览器
Koltin35, headline Android interview algorithm
Scala language learning-07-constructor
谈谈数字化转型的几个关键问题
Provincial election + noi part I dynamic planning DP
SharePoint - modify web application authentication using PowerShell
軟鍵盤高度報錯
OJ输入输出练习
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
web254
window c盘满了
SQL number injection and character injection
数字转excel的字符串坐标
How to check ad user information?
[getting started] input n integers and output the smallest K of them
web254