from typing import ( List,)classSolution:""" @param a: an integer array @param target: An integer @param k: An integer @return: an integer array """defk_closest_numbers(self,a: List[int],target:int,k:int) -> List[int]:# write your code hereifnot a:return [] l,r =0,len(a)-1while l +1< r: mid = (l+r) //2if a[mid]< target : l = midelse: r = mid res = [] i, j = l, l +1#利用m来循环K次,而不是用while+k--,减少错误for m inrange(k):if i <0: res.append(a[j]) j +=1elif j >=len(a): res.append(a[i]) i -=1else:if target - a[i]<= a[j]- target: res.append(a[i]) i -=1else: res.append(a[j]) j +=1return res