class Solution { public: int find(vector<int> res, float a){ int head = 0 , tail = res.size() - 1; int mid = ( tail + head ) / 2; while(tail >= head){ if(res[mid] > a) tail = mid - 1; if(res[mid] < a) head = mid + 1; mid = ( tail + head ) / 2; } return tail; //这里返回头或者尾都行,不要分情况讨论,否则有些情况会差1 } int GetNumberOfK(vector<int> data ,int k) { return find(data,k+0.5) - find(data,k-0.5); } };