class Solution {
public:
int GetNumberOfK(vector<int> data ,int k) {
int lower = getLower(data,k);
int upper = getUpper(data,k);
return upper - lower;
}
int getLower(vector<int> data,int k)
{
int start=0;
int end=data.size()-1;
int mid=(start+end)/2;
while(start<=end)
{
if(data[mid]<k)
{
start=mid+1;
}
else{
end=mid-1;
}
mid = (start+end)/2;
}
return start;
}
int getUpper(vector<int> data,int k)
{
int start=0;
int end=data.size()-1;
int mid=(start+end)/2;
while(start<=end)
{
if(data[mid]<=k)
{
start=mid+1;
}
else{
end=mid-1;
}
mid = (start+end)/2;
}
return end;
}
};
剑指offer 数字在排序数组中出现的次数
最新推荐文章于 2020-02-02 20:07:54 发布