当前位置:网站首页>在排序数组中查找元素的第一个和最后一个位置
在排序数组中查找元素的第一个和最后一个位置
2022-06-13 07:20:00 【Tyno】

public int[] searchRange(int[] nums, int target) {
int [] num = new int[2];
//数组为空的情况
if (nums.length==0){
num[0] = -1;
num[1] = -1;
return num;
}
//二分查找到对应查找的元素,如果没有则返回-1
int flag = commonBinarySearch(nums,target);
//在数组中查找不到元素
if (flag == -1) {
num[0] = -1;
num[1] = -1;
return num;
}
//设置两个指针,指向这个相同元素的头尾
int head = flag;
int rear = flag;
//当到达边界退出
while (head!=0){
// 如果和前一个元素相等,则还需要移动head向前
if (nums[head-1]==nums[head]){
head --;
}else {
// 与前一个元素不相等,找到该元素的最左边界
break;
}
}
//当到达边界退出
while (rear!=nums.length-1){
// 如果和候一个元素相等,则还需要移动rear向前
if (nums[rear]==nums[rear+1]){
rear ++;
}else {
// 与前一个元素不相等,找到该元素的最右边界
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; //定义middle
if (key < arr[low] || key > arr[high] || low > high) {
return -1;
}
while (low <= high) {
middle = (low + high) / 2;
if (arr[middle] > key) {
//比关键字大则关键字在左区域
high = middle - 1;
} else if (arr[middle] < key) {
//比关键字小则关键字在右区域
low = middle + 1;
} else {
return middle;
}
}
return -1; //最后仍然没有找到,则返回-1
}
边栏推荐
- Make cer/pfx public and private key certificates and export CFCA application certificates
- Evolution in the digital age
- [Markov chain Monte Carlo] Markov chain Monte Carlo method sampling prior distribution
- P1434 [SHOI2002] 滑雪 (记忆化搜索
- Powerdispatcher reverse generation of Oracle data model
- [weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background
- RT-Thread 模拟器 simulator LVGL控件:slider 控件
- [turn to] FPGA interview questions
- Introduction and use of dumping
- [RS-422 and RS-485] RS-422 and RS-485 serial interface standard
猜你喜欢

不间断管理设计

Try to use renderdoc to view the shader code of UE

Priority analysis of list variables in ansible playbook and how to separate and summarize list variables

Implementation of fruit mall wholesale platform based on SSM

A solution to the problem that there is always a newline character when C merges multiple RichTextBox contents

对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map

基于FPGA的ds18b20温度传感器使用

Br backup test

First day of learning MySQL Basics

FSM状态机
随机推荐
Xuanwu cloud technology passed the listing hearing: the performance fluctuated significantly, and chenyonghui and other three were the controlling shareholders
10 Honest Facts I Want To Share With All Junior Developers
Word document export
对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
An example of CSRF attack defense in web application scenarios
How to solve the 404 problem
Tidb index optimization
尝试使用RenderDoc查看UE的Shader代码
Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma
A solution to the problem that there is always a newline character when C merges multiple RichTextBox contents
Relevant knowledge under WinForm
Make cer/pfx public and private key certificates and export CFCA application certificates
P1434 [SHOI2002] 滑雪 (记忆化搜索
Issues related to C # delegation and events
2022-06-12:在N*N的正方形棋盤中,有N*N個棋子,那麼每個格子正好可以擁有一個棋子。 但是現在有些棋子聚集到一個格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二維數組代錶,一
Tidb data migration (DM) Introduction
DM Experiment 6: filter replication
JMeter encryption interface test
Tidb statistics
Database connection under WinForm