题目描述:
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
方法一:使用python自带的sort()函数代码超简单 ~~ 但可能面试的时候面试官不允许QQ
# -*- coding:utf-8 -*-
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if not tinput:
return tinput
elif len(tinput) < k:
return []
else:
tinput.sort()
return tinput[:k]
方法二:快速排序