二分法,找到值后左右扩展
class Solution:
def GetNumberOfK(self, data, k):
# write code here
length=len(data)
ll=length
start=0
end=length-1
place=-1
time=0
if length==0:
return 0
else:
i=0;
#end比start小时退出
while start<=end:
#二分
i=(start+end)/2
if data[i]>k:
end=i-1
#找到值,循环
elif data[i]==k:
time=time+1
place=i-1
while place>=0 and data[place]==k:
time=time+1
place=place-1
place=i+1
while place<length and data[place]==k:
time=time+1
place=place+1
return time
elif data[i]<k:
start=i+1
return 0