题解 | #最小的K个数#

最小的K个数

http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf

function GetLeastNumbers_Solution(input, k)
{
// write code here
if(k===0){
return []
}
let res = input.slice(0,k)
let maxIndex = res.indexOf(Math.max(...res))
for(let i=k;i<input.length;i++){
if(input[i]<res[maxIndex]){
res.splice(maxIndex,1,input[i])
maxIndex = res.indexOf(Math.max(...res))
}
}
return res
}
module.exports = {
GetLeastNumbers_Solution : GetLeastNumbers_Solution
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务