题目描述

输入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]

 

相关文章:

  • 2021-07-20
  • 2021-08-08
  • 2021-06-10
  • 2022-12-23
  • 2022-02-25
  • 2021-08-08
  • 2021-04-21
猜你喜欢
  • 2021-08-26
  • 2022-01-27
  • 2022-12-23
  • 2019-11-29
  • 2021-12-15
  • 2022-12-23
  • 2021-07-31
相关资源
相似解决方案