AcWing 53. 最小的k个数
原题链接
简单
作者:
Value
,
2020-09-04 12:06:32
,
所有人可见
,
阅读 305
nlog(n)
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
sort(input.begin(), input.end());
vector<int> res;
for(int i = 0; i < k; i ++ ) res.push_back(input[i]);
return res;
}
};
nlog(k)
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
priority_queue<int> heap;
for(int i = 0; i < input.size(); i ++ ){
heap.push(input[i]);
if(heap.size() > k) heap.pop();
}
vector<int> res;
while(!heap.empty()){
res.push_back(heap.top());
heap.pop();
}
reverse(res.begin(), res.end());
return res;
}
};