思路:快排,二分查找
class Solution {
public:
void quicks(vector<int>& nums,int left,int right,int k){
if(left<=right){
int i=left,j=right;
int x=nums[left];
while(i<j){
while(i<j&&nums[j]>=x) j--;
if(i<j) nums[i]=nums[j];
while(i<j&&nums[i]<=x) i++;
if(i<j) nums[j]=nums[i];
}
nums[i]=x;
if(i==k-1) return ;
else if(i>k-1){
quicks(nums,left,i-1,k);
}
else if(i<k-1){
quicks(nums,i+1,right,k);
}
}
}
vector<int> getLeastNumbers(vector<int>& arr, int k) {
quicks(arr,0,arr.size()-1,k);
return {arr.begin(),arr.begin()+k};
}
};