class Solution {
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
vector<int> vec(k,0);
if(k == 0) return vec;
priority_queue<int> Q;
for(int i = 0;i < k;++i) Q.push(arr[i]);
for(int i = k;i < arr.size();++i){
if(Q.top() > arr[i]){
Q.pop();
Q.push(arr[i]);
}
}
for(int i = 0;i < k;++i){
vec[i] = Q.top();
Q.pop();
}
return vec;
}
};
Leetcode 面试题40. 最小的k个数
最新推荐文章于 2024-08-29 16:23:51 发布