剑指Offer 40 最小的k个数

最小的k个数

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

 1 import heapq
 2 # -*- coding:utf-8 -*-
 3 class Solution:
 4     def GetLeastNumbers_Solution(self, tinput, k):
 5         n = len(tinput)
 6         if k > n:
 7             return []
 8         result = heapq.nsmallest(k, tinput)
 9         return result
10         # write code here
原文地址:https://www.cnblogs.com/asenyang/p/11014210.html