当前位置:网站首页>Find the first and last positions of elements in a sorted array
Find the first and last positions of elements in a sorted array
2022-06-13 07:23:00 【Tyno】

public int[] searchRange(int[] nums, int target) {
int [] num = new int[2];
// When the array is empty
if (nums.length==0){
num[0] = -1;
num[1] = -1;
return num;
}
// Binary search finds the corresponding element , If not, return -1
int flag = commonBinarySearch(nums,target);
// Cannot find element in array
if (flag == -1) {
num[0] = -1;
num[1] = -1;
return num;
}
// Set two pointers , Point to the beginning and end of the same element
int head = flag;
int rear = flag;
// When the border is reached, exit
while (head!=0){
// If it is equal to the previous element , You also need to move head forward
if (nums[head-1]==nums[head]){
head --;
}else {
// Not equal to the previous element , Find the leftmost boundary of the element
break;
}
}
// When the border is reached, exit
while (rear!=nums.length-1){
// If it is equal to an element , You also need to move rear forward
if (nums[rear]==nums[rear+1]){
rear ++;
}else {
// Not equal to the previous element , Find the rightmost boundary of the element
break;
}
}
num[0] = head;
num[1] = rear;
return num;
}
public static int commonBinarySearch(int[] arr,int key) {
int low = 0;
int high = arr.length - 1;
int middle = 0; // Definition middle
if (key < arr[low] || key > arr[high] || low > high) {
return -1;
}
while (low <= high) {
middle = (low + high) / 2;
if (arr[middle] > key) {
// If it is larger than the keyword, the keyword is in the left area
high = middle - 1;
} else if (arr[middle] < key) {
// Smaller than the keyword, the keyword is in the right area
low = middle + 1;
} else {
return middle;
}
}
return -1; // Finally, I still haven't found , Then return to -1
}
边栏推荐
- 基于ESP32CAM实现WebSocket服务器实时点灯
- redis-2. Redis string type & bitmap
- 对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
- JMeter encryption interface test
- [weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background
- [hard copy] core differences among dirty reading, non repeatable reading and unreal reading scenarios
- Can flush open a stock account? Is it safe?
- 5xx series problem solving
- 快速排序
- 5. interrupts and exceptions
猜你喜欢

百货中心供应链管理系统

Try to use renderdoc to view the shader code of UE

socket编程2:IO复用(select && poll && epoll)

Department store center supply chain management system

RT-Thread 模拟器 simulator LVGL控件:button 按钮样式

redis-1. Install redis with pictures and texts

Table access among Oracle database users

Calculate running total / running balance

Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma

Un des backtraders du cadre de quantification lit l'analyseur
随机推荐
C#合并多个richtextbox内容时始终存在换行符的解决方法
redis-2. Redis string type & bitmap
Relevant knowledge under WinForm
Fundamentals of assembly language: register and addressing mode
平衡二叉树学习笔记------一二熊猫
关于#数据库#的问题:PGADMIN4 编辑sql窗口问题
RT-Thread 模拟器 simulator LVGL控件:button 按钮样式
oracle问题,字段里面的数据被逗号隔开,取逗号两边数据
Awk use
How idea breaks point debugging
redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01
汇编语言基础:寄存器和寻址方式
线程池中的 工作线程如何被回收
关于#etl#的问题:io.trino.jdbc.TrinoDriver
Interview questions must be asked - Optimization of large table Pagination
关于oracle的函数。
Deploy RDS service
Lightning breakpoint continuation
Mui mixed development - when updating the download app, the system status bar displays the download progress
RT-Thread 模拟器 simulator LVGL控件:button 按钮事件