题解 | #最小的K个数#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
快排
public:
void QiuckSort(vector<int>& input, int l, int r){
if(l >= r) return;
int x = input[l], i = l - 1, j = r + 1;
while(i < j){
do i++; while(input[i] < x);
do j--; while(input[j] > x);
if(i < j) swap(input[i], input[j]);
}
QiuckSort(input, l, j);
QiuckSort(input, j + 1, r);
}
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
vector<int> ret;
QiuckSort(input, 0, input.size()-1);
for(int i = 0; i < k; ++i){
ret.push_back(input[i]);
}
return ret;
}
};
要时刻记得容器vector的函数