统计一个数字在排序数组中出现的次数。

示例 1:


输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 两个二分法去实现 ​​https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/​


class Solution:
def search(self, nums: [int], target: int) -> int:
# 搜索右边界 right
i, j = 0, len(nums) - 1
while i <= j:
m = (i + j) // 2
if nums[m] <= target:
i = m + 1
else:
j = m - 1
right = i
# 若数组中无 target ,则提前返回
if j >= 0 and nums[j] != target:
return 0
# 搜索左边界 left
i = 0
while i <= j:
m = (i + j) // 2
if nums[m] < target:
i = m + 1
else:
j = m - 1
left = j
return right - left - 1

solu = Solution()
list = [5,7,7,8,8,10]
value = solu.search(list, 7)
print(value)