给定一个长度为 n 的非降序数组和一个非负数整数 k ,要求统计 k 在数组中出现的次数
数据范围:,数组中每个元素的值满足
要求:空间复杂度 ,时间复杂度
要求:空间复杂度 ,时间复杂度
class Solution: # 二分查找 def bi_search(self , data: List[int], k: int) -> int: left = 0 right = len(data) - 1 while left <= right: mid = (left + right) // 2 if data[mid] < k: left = mid + 1 elif data[mid] > k: right = mid - 1 return left def GetNumberOfK(self , data: List[int], k: int) -> int: # 查找k+0.5 和 k-0.5应该出现的位置 return self.bi_search(data, k + 0.5) - self.bi_search(data, k-0.5)
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: id=self.indexof(data, k) if id==-1: return 0 else: return self.find(data,id,k) def indexof(self,data,val): low=0 high=len(data)-1 while low<=high: mid=low+((high-low)>>1) if data[mid]>val: high=mid-1 elif data[mid]<val: low=mid+1 else: return mid return -1 def find(self,arr,p,val): x=[] f=p while f>=0 and arr[f]==val: f-=1 x.append(f+1) while p<len(arr) and arr[p]==val: p+=1 x.append(p) return x[1]-x[0]
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: if not data: return 0 if data[0] > k: return 0 if data[0] == k: count = 0 for i in range(len(data)): if data[i] != k: break count += 1 return count if data[0] < k: mid = len(data) // 2 if mid > 0: return self.GetNumberOfK(data[:mid], k) + self.GetNumberOfK(data[mid:], k) else: return 0二分递归
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: # write code here left = 0 right = len(data) - 1 num = 0 while (left<=right): mid = left + int((right - left) / 2) if data[mid] == k: num += 1 break elif data[mid] < k: left = mid + 1 else: right = mid - 1 if num==0: return 0 else: fleft = mid - 1 fright = mid + 1 while fleft>=0 and (data[fleft]==k): num += 1 fleft -= 1 while fright<=(len(data)-1) and (data[fright]==k): num += 1 fright += 1 return num
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: # write code hereta i=0 j=len(data)-1 if j==0: return 1 if data[0]==k else 0 if not data: return 0 while i<=j: mid=(i+j)//2 if data[mid]<k: i=mid+1 elif data[mid]>=k: j=mid-1 lower=j i=0 j=len(data)-1 while i<=j: mid=(i+j)//2 if data[mid]<=k: i=mid+1 elif data[mid]>k: j=mid-1 return i-lower-1 if i-lower-1>0 else 0
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: # write code here if len(data)==0:return 0 n=len(data) left,right=0,n-1 while left<=right: mid=(left+right)//2 if data[mid]<k: left=mid+1 else: right=mid-1 l=right left,right=0,n-1 while left<=right: mid=(left+right)//2 if data[mid]<=k: left=mid+1 else: right=mid-1 r=left cur=r-l-1 return cur
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: # write code here def find(arr,k): l = 0 r = len(arr) while l<r: mid = int((l+r)/2) if arr[mid] < k: l = mid + 1 else: r = mid return l first = find(data, k) last = find(data, k+1) print(first) print(last) return last - first
class Solution: def GetNumberOfK(self , data: List[int], k: int) -> int: start = 0 left,right=0,len(data)-1 while left<=right: mid = (left+right)//2 if data[mid] < k: left=mid +1 elif data[mid] > k: right=mid -1 elif data[mid] == k: right=mid -1 if left >= len(data)&nbs***bsp;data[left] != k: return 0 start = left left,right=0,len(data)-1 while left<=right: mid = (left+right)//2 if data[mid] < k: left=mid +1 elif data[mid] > k: right=mid -1 elif data[mid] == k: left=mid +1 if right > -1 and data[right] == k: return right - start + 1
def binary_search(alist, item): low = 0 high = len(alist) - 1 while low <= high: mid = (low + high) // 2 if alist[mid] == item: return mid elif alist[mid] > item: high = mid - 1 else: low = mid + 1 return None count=0 w=input() s=int(w[-1]) w=w[1:-3] if len(w)==0: print("0") else: w=list(map(int,w.split(","))) num=binary_search(w,s) if num==None: print("0") else: if w[0]==s: while w[num]>=s and num!=0: num-=1 a=num else: while w[num]>=s: num-=1 a=num+1 if w[len(w)-1]==s: while w[a]==s and a!=len(w)-1: count+=1 a+=1 print(count+1) else: while w[a]==s: count+=1 a+=1 print(count)