求top-k可使用cpp的partial_sort(n.begin(), n.begin()+k, n.end());
该题需要注意边界值的判断
class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> ret; if(k<=0) return ret; if(input.size() <= k) { k = input.size(); } partial_sort(input.begin(), input.begin()+k, input.end()); ret.assign(input.begin(), input.begin()+k); return ret; } };