题解 | #最小的K个数#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
优先队列
import java.util.*; /** * Charley * 2021.07.31 */ public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> list = new ArrayList<>(); PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>(){ public int compare(Integer a, Integer b){ return a - b; } }); Arrays.stream(input).forEach(e -> queue.add(e)); while(k > 0){ list.add(queue.poll()); --k; } return list; } }