一.题目描述
统计一个数字在排序数组中出现的次数。
二.代码(C++)
class Solution {
public:
int GetNumberOfK(vector<int> data ,int k)
{
auto bound = equal_range(data.begin(), data.end(), k);
return bound.second-bound.first;
}
};
三.提交记录
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200622102032373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1VtYnJhbmVy,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200622102054959.png)
四.备注
利用stl算法equal_range 取得该数字在数组中的上下界,算法复杂度是O(log(n))。
或用二分法查找。