当前位置:网站首页>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;
}
边栏推荐
- Li Kou daily question - Day 32 -1822 Symbol of array element product
- Deep learning systematic learning
- Codeworks round 803 (Div. 2) VP supplement
- Scala language learning-07-constructor
- [getting started] intercepting strings
- Source code analysis of open source API gateway APIs IX
- 軟鍵盤高度報錯
- 01 numpy introduction
- 【入门】取近似值
- Stack implementation calculator
猜你喜欢

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

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

【入门】取近似值

使用beef劫持用戶瀏覽器

P4 安装bmv2 详细教程

Gdip - hatchbrush pattern table

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

web254
随机推荐
7-26 word length (input and output in the loop)
Use threejs simple Web3D effect
empirical study and case study
Aardio - [problem] the problem of memory growth during the callback of bass Library
Luogu p1088 [noip2004 popularization group] Martians
How to prevent the other party from saying that he has no money after winning the lawsuit?
Anddroid text to speech TTS implementation
【刷题】字符统计【0】
Download jackson codehaus. org jar - downloading jackson. codehaus. org jar
Airsim radar camera fusion to generate color point cloud
window c盘满了
Codeforces Round #803 (Div. 2) VP补题
Access报表实现小计功能
Aardio - 自己构造的getIconHandle的方法
[getting started] intercepting strings
Codeworks round 803 (Div. 2) VP supplement
Leetcode T40: 组合总和II
Stack implementation calculator
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
[untitled]