题解 | #最小的K个数#
最小的K个数
https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int>::iterator it; vector<int> ans; for(int i=0;i<input.size();i++){ for(it=ans.begin();it!=ans.end();it++){ if(*it>=input[i]){ ans.insert(it,input[i]); if(ans.size()>k){ ans.erase(ans.end()-1); } break; } } if(ans.size()==i){ ans.push_back(input[i]); } if(ans.size()>k){ ans.erase(ans.end()-1); } } return ans; } };