40.【快排+分治】最小的K个数

题目描述

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

思路

方法一:利用优先队列PriorityQueue实现

import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int len = input.length;
        if(k > len||k == 0){
            return list;
        }
        PriorityQueue <Integer>maxHeap = new PriorityQueue<Integer>(k,new Comparator<Integer>(){
            @Override
            public int compare(Integer o1,Integer o2){
                return o2.compareTo(o1);
            }
        });
        for(int i=0;i<len;i++){
            if(maxHeap.size() != k){
               maxHeap.offer(input[i]);     
            }else{
                if(maxHeap.peek()>input[i]){
                    maxHeap.poll();
                    maxHeap.add(input[i]);
                }
            }
        }
        for(Integer i:maxHeap){
            list.add(i);
        }
        return list;
    }
}

 

(简版)

public int[] getLeastNumbers(int[] arr, int k) {
        //定义最小堆
        PriorityQueue <Integer> queue = new PriorityQueue<>((o1,o2) -> o2.compareTo(o1));
        //按顺序将数字存至最小堆中
        for(int i:arr){
            queue.add(i);
            if(!queue.isEmpty() && queue.size() > k) queue.poll();
        }
        //返回结果        
        int res[] = new int[k];
        for(int i=0;i<k;i++){
            res[i] = queue.poll();
        }
        return res;
    }

方法二:

public int[] getLeastNumbers(int[] arr, int k) {
       if(arr == null||arr.length == 0)
           return new int[0];
        int lo = 0, hi = arr.length-1;
        
        while(lo < hi){
            int index = partition(arr, lo, hi);
            if(index == k-1) break;
            else if(index < k-1) lo = index + 1;
            else  hi = index - 1;
        }    
        //返回arr数组,第0到第k-1的k个数
        return Arrays.copyOfRange(arr, 0, k);
    }
    
    public int partition(int []arr, int lo, int hi){
        int pivot = arr[lo];
        int index = lo;
        for(int i=lo; i<= hi; i++){
            if(arr[i] < pivot) swap(arr,i, ++index);
        }
        swap(arr, lo, index);
        return index;       
    }
    
    public void swap(int a[], int i, int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

【参考】

Java中的PriorityQueue的使用方法

https://blog.csdn.net/m0_38103546/article/details/79676669

深入理解PriorityQueue(实现方法)

https://www.cnblogs.com/CarpenterLee/p/5488070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wimb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值