题目描述
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
分析:最直接的思路是排序,排序之后位于最前面的k个数即为所求;
思路一:受到上一题的启发,利用快排一次分区,排到第k个数,则前面k个数就是最小的k个数。
思路二:快排会更改数组,且不适合海量数据。犹记得那年数据结构老师曾提到过,海量数据找最小k个数可以维护一个大顶堆。面试的时候可以给面试官分享一下思路,如何筛选,但是写代码不太实际,因为太多了。可以借助STL的基于红黑树的容器来实现。
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <set>
#include <functional>
using namespace std;
//快排一次分区
int partition(int *a, unsigned int low, unsigned int high)
{
int value = a[low];
while (low < high)
{
while (low < high && value <= a[high])
--high;
if (low < high)
a[low++] = a[high];
while (low < high && value >= a[low])
++low;
if (low < high)
a[high--] = a[low];
}
a[low] = value;
return low;
}
//解法一:可以修改数组、非海量数据;利用快排一次分区
vector<int> GetLeastNumbers(int *numbers, unsigned int length, unsigned int k)
{
vector<int> result;
if (numbers != NULL && length != 0 && k != 0 && k <= length)
{
int index = partition(numbers, 0, length - 1);
while (index != k - 1)
{
if (index > k - 1)
index = partition(numbers, 0, index - 1);
else
index = partition(numbers, index + 1, length - 1);
}
for (unsigned int i = 0; i < k; ++i)
{
result.push_back(numbers[i]);
}
}
return result;
}
//解法二:大顶堆思想,利用STL红黑树解决
void GetLeastNumbers2(const vector<int>& data, multiset<int, greater<int> >& res, unsigned int k)
{
if (data.empty() || k == 0) return;
for (auto it = data.cbegin(); it != data.cend(); ++it)
{
if (res.size() < k)
{
res.insert(*it);
}
else {
if (*it < *(res.begin()))
{
res.erase(res.begin());
res.insert(*it);
}
}
}
}
int main()
{
int numbers[] = {4,5,1,6,2,7,3,8};
vector<int> res = GetLeastNumbers(numbers, 8, 4);
if (res.empty())
printf("error\n");
else
{
for (auto num : res)
{
printf("%d ", num);
}
}
printf("\n");
vector<int> data = {4,5,1,6,2,7,3,8};
multiset<int, greater<int> > res2;
GetLeastNumbers2(data, res2, 4);
for (auto rit = res2.crbegin(); rit != res2.crend(); ++rit)
{
printf("%d ", *rit);
}
printf("\n");
getchar();
return 0;
}
总结:面试的时候应该问清楚具体要求的场合,比如是否是海量数据,能否一次性载入内存,是否允许修改输入数据等等。