先快速排序,再遍历k次,由于限制在10000,所以爱咋咋
代码:
class Solution {
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
vector<int> ans;
sort(arr.begin(), arr.end());
for (int i = 0; i < k; i++)
ans.push_back(arr[i]);
return ans;
}
};
但是时间有点慢