[剑指offer] 最小的K个数

作者: 繁著 | 来源:发表于2018-07-12 16:01 被阅读1次

    本文首发于我的个人博客:尾尾部落

    题目描述

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

    解题思路

    两种方法:

    • 法1:先对数组排序,然后取出前k个
    • 法2:利用最大堆保存这k个数,每次只和堆顶比,如果比堆顶小,删除堆顶,新数入堆。

    参考代码

    法1:

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

    法2:

    import java.util.ArrayList;
    import java.util.PriorityQueue;
    import java.util.Comparator;
    public class Solution {
        public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
            ArrayList<Integer> res = new ArrayList<Integer>();
            if(input == null || k ==0 || k > input.length)
                return res;
            PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() { 
                public int compare(Integer e1, Integer e2) {
                    return e2 - e1;
                }
            });
            for(int i=0; i<input.length; i++){
                if(maxHeap.size() != k)
                    maxHeap.offer(input[i]);
                else{
                    if(maxHeap.peek() > input[i]){
                        maxHeap.poll();
                        maxHeap.offer(input[i]);
                    }
                }
            }
            for(Integer i: maxHeap){
                res.add(i);
            }
            return res;
        }
    }
    

    相关文章

      网友评论

        本文标题:[剑指offer] 最小的K个数

        本文链接:https://www.haomeiwen.com/subject/wldrpftx.html