最小的K个数

Desicription

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

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
if(input.size() < k) {
return vector<int>(0);
}
auto compare = [](int a, int b) {
return b > a;
};
priority_queue<int, vector<int>, decltype(compare)> priorityQueue(compare);
for(auto number : input) {
priorityQueue.push(number);
while(priorityQueue.size() > k) {
priorityQueue.pop();
}
}
vector<int> res;
while(!priorityQueue.empty()) {
res.push_back(priorityQueue.top());
priorityQueue.pop();
}
return res;
}
};