优先队列
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;
}
}
京公网安备 11010502036488号