剑指Offer26:最小的K个数

思路:

先判断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]

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/84982559