题解 | #最小的K个数#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { if (k > input.size()) return {}; vector<int> res; priority_queue<int> pq; // 大根堆 for (int i = 0; i < input.size(); i++) { pq.push(input[i]); if (pq.size() > k) pq.pop(); } while (!pq.empty()) { res.push_back(pq.top()); pq.pop(); } return res; } };