两个二分板子
class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
int len = nums.size();
if(len == 0)return 0;
int first = findFirst(nums, k);
if(nums[first] != k)return 0;
int last = findLast(nums, k);
return last - first + 1;
}
int findFirst(vector<int>& nums, int x){
int l = 0, r = nums.size()-1;
while(l < r){
int mid = l+r>>1;
if(nums[mid] >= x)r = mid;
else l = mid + 1;
}
return l;
}
int findLast(vector<int>& nums, int x){
int l = 0, r = nums.size()-1;
while(l < r){
int mid = l+r+1>>1;
if(nums[mid] <= x)l = mid;
else r = mid-1;
}
return l;
}
};