剑指offer——python【第29题】最小的K个数

题目描述

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

思路

先排序后取数,排序可以用冒泡,插入,选择,快排,二分法等等,或者直接用sorted函数

解答

class Solution:
    def GetLeastNumbers_Solution(self, tinput, k):
        # write code here
        return [] if not tinput or k>len(tinput) else sorted(tinput)[:k]

猜你喜欢

转载自www.cnblogs.com/yqpy/p/9572798.html
今日推荐