# 找出一堆数据中最小的K个数问题

1852人阅读 评论(1)

#include <set>
#include <vector>
#include <iostream>

using namespace std;

typedef multiset<int, greater<int> > IntHeap;

///////////////////////////////////////////////////////////////////////
// find k least numbers in a vector
///////////////////////////////////////////////////////////////////////
void FindKLeastNumbers
(
const vector<int>& data,               // a vector of data
IntHeap& leastNumbers,                 // k least numbers, output
unsigned int k
)
{
leastNumbers.clear();

if(k == 0 || data.size() < k)
return;

vector<int>::const_iterator iter = data.begin();
for(; iter != data.end(); ++ iter)
{
// if less than k numbers was inserted into leastNumbers
if((leastNumbers.size()) < k)
leastNumbers.insert(*iter);

// leastNumbers contains k numbers and it's full now
else
{
// first number in leastNumbers is the greatest one
IntHeap::iterator iterFirst = leastNumbers.begin();

// if is less than the previous greatest number
if(*iter < *(leastNumbers.begin()))
{
// replace the previous greatest number
leastNumbers.erase(iterFirst);
leastNumbers.insert(*iter);
}
}
}
}

//补充的代码

/*

1、最简单的办法，给所有数据按升序或降序排序，从中取出相应的k个就可以了，最快的时间复杂度为nlog(n)
2、可以考虑维护一个堆，这个堆的中元素的个数为k，然后遍历元素，这时有两种情况：
1、堆中元素的个数小于k，这时直接插入即可
2、元素的个数为k，这时如果遍历到的元素小于堆中最大的那个元素就将堆中的这个元素删除，将遍历到的元素插入即可
*/

#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef multiset<int, greater<int> > intHeap;

void FindNumbers(vector<int> &data, int k, intHeap &numbers)
{
numbers.clear();
if (0==k || data.size()<k)
return ;
vector<int>::const_iterator iter = data.begin();
for (; iter!=data.end(); iter++)
{
if (numbers.size() < k)
{
numbers.insert(*iter);
}
else
{
intHeap::iterator heapIter = numbers.begin();
if (*iter < *(numbers.begin()))
{
numbers.erase(heapIter);
numbers.insert(*iter);
}
}
}
}

int main()
{
int n;
vector<int> data;
intHeap numbers;
while (cin>>n)
{
for (int i=0; i<n; ++i)
{
int nTemp;
cin>>nTemp;
data.push_back(nTemp);
}
FindNumbers(data, 4, numbers);
intHeap::const_iterator iter = numbers.begin();
for (; iter!=numbers.end(); ++iter)
{
cout<<*iter<<" ";
}
cout<<endl;
}
return 0;
}
//我的借鉴方法

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>

using namespace std;

//print an array of int
inline void print(int *arr, //the array of int
int n //the length of array
)
{
for(int i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;

}
typedef multiset<int, greater<int> > IntHeap;

///////////////////////////////////////////////////////////////////////
// find k least numbers in a vector
///////////////////////////////////////////////////////////////////////
void FindKLeastNumbers(
//   vector<int>& data,             // a vector of data
int *array,
unsigned int len,
IntHeap& leastNumbers,                 // k least numbers, output
unsigned int k )
{

std::vector <int> data(array, array + len);

leastNumbers.clear();

if(k == 0 || data.size() < k)
return;

vector<int>::const_iterator iter = data.begin();
for(; iter != data.end(); ++ iter)
{
// if less than k numbers was inserted into leastNumbers
if((leastNumbers.size()) < k)
leastNumbers.insert(*iter);

// leastNumbers contains k numbers and it's full now
else
{
// first number in leastNumbers is the greatest one
IntHeap::iterator iterFirst = leastNumbers.begin();

// if is less than the previous greatest number
if(*iter < *(leastNumbers.begin()))
{
// replace the previous greatest number
leastNumbers.erase(iterFirst);
leastNumbers.insert(*iter);
}
}
}
}

void main()
{
int arr[ 10 ] = {3, 4, 8, 6, 0, 1, 5, 7, 2, 9};

int len = sizeof(arr) / sizeof(int);

int LeastNumers = 5;

IntHeap numbers;

FindKLeastNumbers(arr,len,numbers,LeastNumers);

IntHeap::const_iterator iter = numbers.begin();
for (; iter != numbers.end(); ++iter)
{
cout<<*iter<<" ";
}
cout<<endl;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1532846次
• 积分：18219
• 等级：
• 排名：第527名
• 原创：531篇
• 转载：184篇
• 译文：6篇
• 评论：131条
最新评论
评论排行