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

全部评论

相关推荐

10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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