数字在排序数组中出现的次数 牛客网 剑指Offer
题目描述统计一个数字在排序数组中出现的次数。 class Solution: def GetNumberOfK(self, data, k): if data == None or data == []: return 0 if len(data) == 1 and k!=data[0]: return 0 lenA = len(data) count = 0 if data[lenA/2] > k: return self.GetNumberOfK(data[:lenA/2],k) elif data[lenA/2] < k: return self.GetNumberOfK(data[lenA/2:],k) else: for i in range(lenA/2,lenA): if data[i] == k: count +=1 else: break for i in range(lenA/2-1,-1,-1): if data[i] == k: count +=1 else: break return count
转载于:https://www.cnblogs.com/vercont/p/10210379.html