思路:
// @Title: 在排序数组中查找元素的第一个和最后一个位置 (Find First and Last Position of Element in Sorted Array)
// @Author: qisiii
// @Date: 2024-04-25 23:48:56
// @Runtime: 0 ms
// @Memory: 44.8 MB
// @comment:
// @flag:
class Solution {
public int[] searchRange(int[] nums, int target) {
int left=0,right=nums.length-1;
int find=-1;
while(left<=right){
int mid=left+(right-left)/2;
if(nums[mid]==target){
find=mid;
break;
}else if(nums[mid]<target){
left=mid+1;
}else{
right=mid-1;
}
}
if(find==-1){
return new int[]{-1,-1};
}else{
int l=find,r=find;
while(l>=0&&nums[l]==target){
l--;
}
while(r<=right&&nums[r]==target){
r++;
}
return new int[]{l+1,r-1};
}
}
}