1、顺序遍历

2、二分查找

# 代码：

#include <iostream>

using namespace std;

int getFirstK(int* data,int k,int start,int end){
while(start<=end){
int mid=start+((end-start)>>1);
if(data[mid]==k){
if((mid>0 && data[mid-1]!=k) || mid==0)
return mid;
else
end=mid-1;
}
else if(data[mid]>k)
end=mid-1;
else
start=mid+1;
}
return -1;
}

int getLastK(int* data,int length,int k,int start,int end){
while(start<=end){
int mid=start+((end-start)>>1);
if(data[mid]==k){
if((mid<length-1 && data[mid+1]!=k) || mid==length-1)
return mid;
else
start=mid+1;
}
else if(data[mid]<k)
start=mid+1;
else
end=mid-1;
}
return -1;
}

int getNumberOfK(int* data,int length,int k){
if(data==NULL || length<=0)
return 0;
int first=getFirstK(data,k,0,length-1);
int last=getLastK(data,length,k,0,length-1);
cout<<first<<" "<<last<<endl;
if(first!=-1 && last!=-1)
return last-first+1;
return 0;
}

int main()
{
int A[]={1,2,3,3,3,3,4,5};
int len=sizeof(A)/sizeof(A[0]);
int k=3;
cout << getNumberOfK(A,len,k) << endl;
return 0;
}


# 在线测试OJ：

http://www.nowcoder.com/books/coding-interviews/70610bf967994b22bb1c26f9ae901fa2?rp=2

AC代码：

class Solution {
public:
int GetNumberOfK(vector<int> data,int k) {
int len=data.size();
if(len<=0)
return 0;
int first=getFirstK(data,k,0,len-1);
int last=getLastK(data,len,k,0,len-1);

if(first!=-1 && last!=-1)
return last-first+1;
return 0;
}

int getFirstK(const vector<int> &data,int k,int start,int end){
int mid;
while(start<=end){
mid=start+((end-start)>>1);
if(data[mid]==k){
if((mid>0 && data[mid-1]!=k) || mid==0)
return mid;
else
end=mid-1;
}
else if(data[mid]>k)
end=mid-1;
else
start=mid+1;
}
return -1;
}

int getLastK(const vector<int> &data,int length,int k,int start,int end){
int mid;
while(start<=end){
mid=start+((end-start)>>1);
if(data[mid]==k){
if((mid<length-1 && data[mid+1]!=k) || mid==length-1)
return mid;
else
start=mid+1;
}
else if(data[mid]>k)
end=mid-1;
else
start=mid+1;
}
return -1;
}
};
posted @ 2015-07-26 18:16  AndyJee  阅读(...)  评论(...编辑  收藏