最小的k个数

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

解法一:

对整个数组进行排序,排序后直接取前k个数。

代码如下:

public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> result = new ArrayList<>();
        if (input == null || input.length < k){
            return result;
        }
        Arrays.sort(input);
        for (int i = 0; i < k; i++) {
            result.add(input[i]);
        }
        return result;
    }


解法二:

利用快速排序的基础算法来实现,当我们取数组中第k-1的数,假如比它小的数都在它的左边,则前k个数就是数组中最小的k个数

代码如下:

    public static void swap(int[] input, int x, int y){
        int t = input[x];
        input[x] = input[y];
        input[y] = t;
    }
    public static int partition(int[] input, int start, int end){
        if (input == null ||
                input.length <= 0 ||
                start < 0 ||
                end >= input.length){
            return -1;
        }
        int index = new java.util.Random()
                .nextInt(end - start) + start;
        swap(input, index, end);
        int small = start - 1;
        //遍历数组段
        for (index = start; index < end; index++) {
            if (input[index] < input[end]){
                small ++;
                if (small != index){
                    swap(input, index, small);
                }
            }
        }
        ++ small;
        swap(input, small, end);
        return small;
    }
    public static ArrayList<Integer> GetLeastNumbers_Solution2(int [] input, int k) {
        ArrayList<Integer> result = new ArrayList<>();
        if (input == null || input.length < k || k <= 0){
            return result;
        }
        if (k != input.length){
            int start = 0;
            int end = input.length - 1;
            int index = partition(input, start, end);
            while (index != k - 1){
                if (index > k - 1){
                    end = index - 1;
                    index = partition(input, start, end);
                }else{
                    start = index + 1;
                    index = partition(input, start, end);
                }
            }
        }
        Arrays.sort(input, 0, k);
        for (int i = 0; i < k; i++) {
            result.add(input[i]);
        }
        return result;
    }

解法三:

如果有海量的数据,我们创建一个大小为k的辅助空间,同时标志最大值,当我们从头到尾遍历整个输入数组,当辅助空间未满,则直接放入辅助空间,如果满了而且当前遍历值小于辅助空间最大值,则替换辅助空间最大值,同时查找最大值。

代码如下:

    public static ArrayList<Integer> GetLeastNumbers_Solution3(int [] input, int k) {
        ArrayList<Integer> result = new ArrayList<>();
        if (input == null || input.length < k || k <= 0){
            return result;
        }
        int max = 0;
        if (k != input.length){
            for (int i = 0; i < input.length; i++) {
                if (result.size() < k){
                    result.add(input[i]);
                    if (result.get(max) < input[i]){
                        max = result.size() - 1;
                    }
                }else if (result.get(max) > input[i]){
                    result.set(max, input[i]);
                    max = 0;
                    for (int j = 1; j < result.size(); j++) {
                        if (result.get(max) < result.get(j)){
                            max = j;
                        }
                    }
                }
            }
        }else{
            for (int i = 0; i < k; i++) {
                result.add(input[i]);
            }
        }
        Collections.sort(result);
        return result;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值