当前位置:网站首页>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
}
边栏推荐
- 汇编语言基础:寄存器和寻址方式
- C drawing table and sending mail function
- The password does not take effect after redis is set
- How is it that the income of financial products is zero for several consecutive days?
- Reflection of C # Foundation
- 【splishsplash】重复输出splashsurf的脚本
- ISIS的vsys(虚拟系统)
- redis-2. Redis string type & bitmap
- 比较DFS和BFS的优点和缺点及名称词汇
- 5. interrupts and exceptions
猜你喜欢

Real time lighting of websocket server based on esp32cam

Calculate running total / running balance

Compilation and development process of Quanzhi v3s environment

TiDB Lightning

Through the function seaborn cubehelix_ Palette build order palette

redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01

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

RT-Thread 模拟器 simulator LVGL控件:switch 开关按钮控件

redis-5. Redis' RDB, fork, copyonwrite, AOF, RDB & AOF are mixed

NFV基本概述
随机推荐
How to stop PHP FPM service in php7
Interview questions must be asked - Optimization of large table Pagination
理財產品連續幾天收益都是零是怎麼回事?
Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma
10 Honest Facts I Want To Share With All Junior Developers
考研英语
[turn to] FPGA interview questions
Personal JS learning notes
Performance tuning can't just depend on tapping the brain
Functions about Oracle.
Ansible PlayBook的中清单变量优先级分析及清单变量如何分离总结
不同系统添加证书
全志V3S环境编译开发流程
Sorting of numbers and strings
P1434 [SHOI2002] 滑雪 (记忆化搜索
Number of detection cycles "142857“
The biggest highlight of wwdc2022: metalfx
C drawing table and sending mail function
RT thread simulator lvgl control: slider control
P6154 wandering (memory search