题解 | #最小的K个数 堆#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
//优先队列就是小根堆
PriorityQueue<Integer> heap = new PriorityQueue<>();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < input.length; i++) {
heap.offer(input[i]);
}
for (int i = 0; i < k; i++) {
list.add(heap.poll());
}
return list;
}
}