题解 | #最小的K个数#
最小的K个数
https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
时间复杂度:O(kn)
空间复杂度: O(n)
function GetLeastNumbers_Solution(input, k) { // write code here if (k >= input.length) return input const result = [] let subInput = input while (result.length < k) { const rest = [] let min = subInput[0] for (let i = 1; i < subInput.length; i++) { if (subInput[i] < min) { rest.push(min) min = subInput[i] } else { rest.push(subInput[i]) } } result.push(min) subInput = rest } return result } module.exports = { GetLeastNumbers_Solution : GetLeastNumbers_Solution };