题解 | #最小的K个数#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
直接考虑用堆来排序。 python还自带的函数。直接计算出结果。
import heapq
class Solution:
def GetLeastNumbers_Solution(self , input: List[int], k: int) -> List[int]:
# write code here
input.sort()
return input[0:k]
if k == 0 :
return None
heapq.heapify(input)
ans = heapq.nsmallest(k,input)
return ans