题解 | #最小的K个数#

最小的K个数

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

  • 简单选择排序

public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        int minIndex = 0;
        int temp = 0;
        ArrayList<Integer> returnList = new ArrayList<>();
        for(int i = 0; i < k; i++){
            minIndex = i;
            for(int j = i+1; j< input.length; j++){
                if(input[minIndex] >= input[j]){
                    minIndex = j;
                }
            }
            returnList.add(input[minIndex]);
            temp = input[i];
            input[i] = input[minIndex];
            input[minIndex] = temp;
        }
        
        return returnList;
    }
}
全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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