Old Wei wins the offer to take you to learn --- Brush title series (K smallest number 29.)

29. The minimum number of K

problem:

Input n integers, find the smallest number K. 4,5,1,6,2,7,3,8 e.g. eight digital inputs, the minimum number is four 1,2,3,4 ,.

solve:

thought:

This problem simple reason, to sort the array first, then to remove the first k number.

python code:

# -*- coding:utf-8 -*-
class Solution:
    def GetLeastNumbers_Solution(self, tinput, k):
        # write code here
        n=len(tinput)
        if(k>n):
            return []
        for i in range(n-1):
            for j in range(n-i-1):
                if tinput[j]>tinput[j+1]:
                    tinput[j],tinput[j+1]=tinput[j+1],tinput[j]
        return tinput[0:k]
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104964998