思路:
先判断k是否大于数组的长度,若是则返回空。反之先将数组排序,然后选出前k个元素。
# -*- coding:utf-8 -*-
class Solution:
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if k > len(tinput):
return []
else:
sorttinput=sorted(tinput)
return sorttinput[0:k]