class Solution {
public:
int GetNumberOfK(vector<int> data ,int k) {
if (data.empty()) {
return 0;
}
// 升序数组
int left = 0, right = data.size() - 1, mid = 0;
int res = 1;
while (left <= right) {
mid = left + (right - left) / 2;
if (data[mid] == k) {
break;
} else if (data[mid] > k) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (left > right) {
return 0;
}
left = right = mid;
--left, ++right;
while (left >= 0 && data[left--] == k) {
++res;
}
while (right < data.size() && data[right++] == k) {
++res;
}
return res;
}
};