题解 | #最小的K个数#

最小的K个数

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

import java.util.ArrayList;

public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> resList = new ArrayList<>();
        if (input == null||input.length==0) {
            return resList;
        }

        quickSortList(input, 0, input.length - 1);

        for (int i = 0; i < k; i++) {
            resList.add(input[i]);
        }

        return resList;
    }


    public void quickSortList(int[] input, int low, int high) {
        int pow = quickSort(input, low, high);
        if(pow>low){
           quickSortList(input, low, pow-1);
        }
        if(pow<high){
            quickSortList(input, low + 1, high);
        }
    }

    public int quickSort(int[] input, int low, int high) {
        int piot = input[low];
        while (low < high) {
            while (low < high && input[high] >= piot) {
                --high;
            }

            input[low] = input[high];

            while (low < high && input[low] <= piot) {
                ++low;
            }
            input[high] = input[low];
        }
        input[low] = piot;
        return low;
    }
}

#我的求职思考##在找工作求抱抱#
全部评论

相关推荐

迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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