当前位置:网站首页>34. find the first and last positions of elements in the sorted array - binary search, double pointer
34. find the first and last positions of elements in the sorted array - binary search, double pointer
2022-06-10 23:17:00 【hequnwang10】
One 、 Title Description
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]
Two 、 Problem solving
Two points search
This problem needs to be advanced O(log(n)) Time complexity of , So I thought of binary search , If you find a target number in an ascending non repeating array, it is easier to write , Here the array numbers are repeated , So it can be divided into finding the position where the target value first appears in the array , And then looking for the second place , However, the position of the second occurrence can be changed to the target value +1 The position of the first occurrence in the array , Then the position is -1.
class Solution {
public int[] searchRange(int[] nums, int target) {
// Sorted array log(n) Two points search
if(nums == null || nums.length == 0){
return new int[]{
-1,-1};
}
int start = binsearch(nums,target);
int end = binsearch(nums,target+1)-1;
// Yes start and end Make boundary judgment
if(start == nums.length || nums[start] != target){
return new int[]{
-1,-1};
}else{
return new int[]{
start,end};
}
}
// Two points search
public int binsearch(int[] nums,int target){
int left = 0,right = nums.length;
while(left < right){
int mid = left + (right - left) / 2;
// If the median value is less than the target value Look to the right
if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] == target){
// The target value is equal to the median value Find on the left
right = mid;
}else{
// Look to the left
right = mid;
}
}
return left;
}
}
Traverse left and right
Double pointer traversal , Define two pointers to traverse from left to right and from right to left respectively , If you find the value that appears for the first time , Record the subscript .
class Solution {
public int[] searchRange(int[] nums, int target) {
// Traverse left and right
int[] res = new int[]{
-1,-1};
int left = 0,right = nums.length - 1;
if(nums == null || nums.length == 0){
return res;
}
// Traverse from left to right
for(int i = left;i<=right;i++){
if(nums[i] == target){
res[0] = i;
break;
}
}
// Traverse from right to left
for(int i = right;i>=left;i--){
if(nums[i] == target){
res[1] = i;
break;
}
}
return res;
}
}

边栏推荐
- 2022g1 industrial boiler stoker test questions and online simulation test
- Project training 12 - parsing SQL statements for creating tables
- ICML2022 | 從零開始重新審視端到端的語音到文本翻譯
- Model Workshop
- Electronic Society C language level 1 7. Draw rectangle
- About the college entrance examination
- 项目实训10——对特定数据库的备份
- 关于String.format(String format, Object... args)
- 28岁自学编程会不会太晚了?靠谱吗?
- Ride the storm and explore the secret behind the open source of flyfish, a data visualization development platform!
猜你喜欢

Laravel8 enables alicloud file upload

Display of successful cases of target customer matching data table

数据与信息资源共享平台(六)

28岁自学编程会不会太晚了?靠谱吗?

Icml2022 | reexamine end-to-end voice to text translation from scratch
![[original] analysis of nine price HPV data capture of Yilu app](/img/e0/af4901d119a6e59de02a6786d427dd.png)
[original] analysis of nine price HPV data capture of Yilu app

Model Workshop

字蛛(font-spider)教学——ttf/otf字体文件压缩

Auto.js pro 开发环境配置

集度夏一平:不是所有事都向李彦宏汇报,靠产品跟小米华为竞争
随机推荐
[play with Huawei cloud] take you through the Kunpeng code migration tool to realize source code migration
Electronic Society C language level 1 7. Draw rectangle
Vulnhub's DC3
启牛的证券是哪个证券公司的呢?安全吗?
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
优化代码去除if-else
[Interface tutorial] how does easycvr set platform cascading through the interface?
集度夏一平:不是所有事都向李彦宏汇报,靠产品跟小米华为竞争
开中银证券账户安全吗?风险高吗?
vulnhub之DC2
【接口教程】EasyCVR如何通过接口设置平台级联?
线程池——治理线程的法宝
Sealem Finance-基于Web3的全新去中心化金融平台
MySQL mvcc multi version concurrency control
数据文件 Insurance csv包含1338条观测,即目前已经登记过的保险计划受益者以及表示病人特点和历年计划入的总的医疗费用的特征。这些特征是
关于idea中src下 无法new一个package
Array, list, set, map, properties dependency injection format
leetcode 130. Surrounded regions (medium)
Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products
Auto.js pro 开发环境配置