leetcode刷题笔记29

面试题53 - I. 在排序数组中查找数字 I

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

class Solution:
    def search(self, nums: List[int], target: int) -> int:
    	i,j = 0,len(nums) -1
    	while i<=j:
    		mid = (i+j)//2
    		if nums(mid)<=target:	i = mid + 1
    		if nums(mid)>target: j = mid-1
    	right = i
     	i,j = 0,len(nums) -1
     	while i<=j:
      		mid = (i+j)//2
      		if nums(mid)<=target: i = mid + 1
      		if nums(mid)>target: j = mid-1
      	left = j
      	return right - left -1
   

你可能感兴趣的:(leetcode刷题笔记29)