题解 | #最小的K个数#

最小的K个数

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

利用选择排序法
import java.util.ArrayList;

public class Solution {
public ArrayList<integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<integer> res = new ArrayList<>();
if(k == 0) return res;
else
for(int i = 0;i<k;i++){
int minIndex = i;
for(int j = i+1;j<input.length;j++){
if(input[minIndex]>input[j]) minIndex = j;
}
if(minIndex != i){
int temp = input[i];
input[i] = input[minIndex];
input[minIndex] = temp;
}
res.add(input[i]);
}
return res;
}
}</integer></integer>

全部评论

相关推荐

迷茫的大四🐶:都收获五个了,兄弟那还说啥,不用改了,去玩吧
点赞 评论 收藏
分享
用微笑面对困难:你出于礼貌叫了人一声大姐,大姐很欣慰,她真把你当老弟
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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