题解 | #最小的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
};

全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务