题解 | #最小的K个数#

最小的K个数

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

import java.util.ArrayList;

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

相关推荐

不愿透露姓名的神秘牛友
03-26 16:16
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务