JZ72 数字在升序数组中出现的次数

class Solution:
    def GetNumberOfK(self, data, k):
        l = 0
        r = len(data)-1
        while l <= r:
            m = l + (r - l)//2
            if data[m] > k:
                r = m - 1
            elif data[m] < k:
                l = m + 1
            else:
                l = m + 1
        right = l
        l = 0
        while l <= r:
            m = l + (r - l)//2
            if data[m] < k:
                l = m + 1
            else:
                r = m - 1
        return right - r - 1 
class Solution:
    def GetNumberOfK(self, data, k):
        # write code here
        count = 0
        for i in range(len(data)):
            if data[i] == k:
                count +=1
        return count
上一篇:SA求解TSP


下一篇:TIS2000 “E666 Access to SPS Server was Denied” Solution