vector<int> searchRange(vector<int>& nums, int target) {
int left=0;
int right=nums.size()-1;
vector<int> res(2,-1);
while(left<=right){
int mid=left+(right-left)/2;
if(nums[mid]==target){
int l=mid;
int r=mid;
while(l>=0&&nums[l]==target) l--; // 注意边界值
while(r<=(nums.size()-1)&&nums[r]==target) r++;
res[0]=l+1;
res[1]=r-1;
return res;
}else if(nums[mid]<target){
left=mid+1;
}else{
right=mid-1;
}
}
return res;
}
有序序列查找一定的范围
最新推荐文章于 2023-09-23 14:27:34 发布