class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
if(nums.empty()) return 0;
int n=nums.size();
int res=0;
//--------------------------------------------- 寻找第一次出现的位置
int l=0,r=n-1;
while(l<r){
int mid =l+r>>1;
if(nums[mid]<k) l=mid+1;
else r = mid;
}
if(nums[l]!=k) return 0;
int a =l;
// -------------------------------------------- 寻找最后一次出现的位置
l=0,r=n-1;
while(l<r){
int mid =l+r+1>>1;
if(nums[mid]<=k) l=mid;
else r = mid-1;
}
int b = l;
//------------------------------------------------
res=b-a+1;
return res;
}
};