Esempio n. 1
0
void GetLeastNumbers(const vector<int>& a, intset& leastNum, size_t k)
{
	leastNum.clear();
	if (k < 1 || a.size() < k)
		return;
	vector<int>::const_iterator iter = a.begin();
	for (; iter != a.end(); iter++)
	{
		if ((leastNum.size()) < k)
			leastNum.insert(*iter);
		else
		{
			setiterator itergreater = leastNum.begin();
			if (*iter < *(leastNum.begin()))
			{
				leastNum.erase(itergreater);
				leastNum.insert(*iter);
			}
		
		}
	}
}
Esempio n. 2
0
void
solution2(const vector<int>& data, intset& leastNumbers, int k)
{
    leastNumbers.clear();

    if (k<1 && data.size()<k)
        return ;

    vector<int>::const_iterator iter = data.begin();
    for (; iter != data.end(); ++iter) {
        if ((leastNumbers.size()) < k)
            leastNumbers.insert(*iter);
        else {
            setIterator iterBig = leastNumbers.begin();

            if (*iter < *iterBig) {
                leastNumbers.erase(iterBig);  
                leastNumbers.insert(*iter);
            }
        }
    }
}