剑指offer 30题 【时间效率】最小的K个数

题目描述

输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
牛客传送门:点击打开链接

思路一:不断地利用快排partition函数,当index == k-1时结束。(注意这里是k-1,因为index表示数组位置)
思路二:利用优先队列。
代码一:
public class Title30 {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> result = new ArrayList<Integer>(k);
        if(input == null || input.length == 0 || k<=0 || k > input.length)
            return result;

        int index = -1,begin = 0,end = input.length - 1;
        k--;
        while(index != k){
            index = partition(input,begin,end);
            if(index > k)
                end = index - 1;
            else if(index < k)
                begin = index + 1;
        }
        
        quickSort(input,0,k);
        for(int i=0;i<=k;i++){
            result.add(input[i]);
        }
        return result;
    }
    
    public void quickSort(int[] array,int begin,int end){
        if(begin < end){
            int index = partition(array,begin,end);
            quickSort(array,begin,index-1);
            quickSort(array,index+1,end);
        }
    }
    
    public int partition(int[] array,int begin,int end){
        int small = begin-1;
        for(int i=begin;i<end;i++){
            if(array[i] < array[end]){
                ++small;
                if(i != small)
                    swap(array,i,small);
            }
        }
        
        ++small;
        swap(array,small,end);
        return small;
    }
    
    public void swap(int[] array,int posA,int posB){
        if(posA == posB)
            return ;
        array[posA] = array[posA] + array[posB];
        array[posB] = array[posA] - array[posB];
        array[posA] = array[posA] - array[posB];
    }

    public static void main(String[] args) {
        int[] a = {4,5,1,6,2,7,3,8};
        System.out.println(new Title30().GetLeastNumbers_Solution(a, 8));
        System.out.println(new Title30().GetLeastNumbers_Solution(a, 4));
    }
}

代码二:
public class Title30 {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> result = new ArrayList<Integer>(k);
        if(input == null || input.length == 0 || k<=0 || k > input.length)
            return result;
        // 优先队列
        PriorityQueue<Integer> priQueue = new PriorityQueue<Integer>(k, new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }});
        
        for(int num : input){
            // 如果队列满了,则将这个数与队列的最大值比较。
            if(priQueue.size() == k){
                if(num < priQueue.peek()){
                    priQueue.poll();
                    priQueue.add(num);
                }
            }else{
                priQueue.add(num);
            }
        }
            
        result.addAll(priQueue);
        Collections.sort(result);
        return result;
    }

    public static void main(String[] args) {
        int[] a = {4,5,1,6,2,7,3,8};
        System.out.println(new Title30().GetLeastNumbers_Solution(a, 8));
        System.out.println(new Title30().GetLeastNumbers_Solution(a, 4));
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值