注释为注意事项
class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> res; if(!input.size()||k==0) return res; priority_queue<int> pq; for(int i =0;i< input.size();i++){ if(pq.size()==k){//注意灵活变通 if(input[i]< pq.top()){ pq.pop(); pq.push(input[i]); } }else{ pq.push(input[i]); } } while(!pq.empty()){ res.push_back(pq.top()); pq.pop(); } sort(res.begin(),res.end()); return res;
}
};
```