The sword refers to the minimum k number of the twenty-ninth question of the offer

Topic description

Enter n integers and find the smallest K number among them. For example, if you enter 8 numbers 4,5,1,6,2,7,3,8, the smallest 4 numbers are 1,2,3,4,.

topic analysis

The topic is changed, it is simple and easy to do, just go to the code. Can library function sort, quicksort idea and heap sort

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        int size=input.size();
        vector<int> ret;
        if(input.empty()||k>input.size()) return ret;
        sort(input.begin(),input.end());
        for(int i=0;i<k;i++){
            ret.push_back(input[i]);
        }
        return ret;
    }
};
Quick row
class Solution {
public:
    void swap(int &fir,int &sec)
    {
        int temp = fir;
        fir = sec;
        sec = temp;
    }
     
    int getPartition(vector<int> &input,int start,int end)
    {
        if(input.empty() || start>end) return -1;
        int temp = input[end];
        int j = start - 1;
        for(int i=start;i<end;++i)
        {
            if(input[i]<=temp)
            {
                ++j;
                if(i!=j) swap(input[i],input[j]);                   
            }
        }
        swap(input[j+1],input[end]);
        return (j+1);
    }
         
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
    {
        vector<int> result;       
        if(input.empty() || k>input.size() || k<=0) return result;
         
        int start = 0;
        int end = input.size()-1;
        int index = getPartition(input,start,end);
         
        while(index != (k-1))
        {
            if(index > (k-1))
            {
                end = index - 1;
                index = getPartition(input,start,end);
            }
            else
            {
                start = index + 1;
                index = getPartition(input,start,end);
            }
        }
         
        for(int i=0;i<k;++i)
        {
            result.push_back(input[i]);
        }
         
        return result;
    }
};
heap sort
class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
    {
        vector<int> result;
        int len = input.size();
        if(input.empty() || k<=0 || len < k) return result;
         
        multiset<int, greater<int> > leastNumbers; // sort from largest to smallest
        multiset<int, greater<int> >::iterator iterGreater; // define the iterator
         
        vector<int>::iterator iter = input.begin();
        for(; iter != input.end(); ++iter)
        {
            // Insert the first k numbers directly into the multiset, note that it is less than K
            if(leastNumbers.size() < k)
            {
                leastNumbers.insert(*iter);
            }
            else
            {
                // Because the setting is sorted from large to small, the element in the first position in the multiset is the maximum value
                iterGreater = leastNumbers.begin();
                 
                // If the current element in the input is smaller than the largest element in the multiset, replace it; that is, keep the k elements in the multiset as the smallest.
                if(*iter < *(leastNumbers.begin()))
                {
                    // replace the current maximum value
                    leastNumbers.erase(iterGreater);
                    leastNumbers.insert(*iter);
                }
            }
        }
         
        for(iterGreater = leastNumbers.begin();iterGreater!=leastNumbers.end();++iterGreater)
        {
            result.push_back(*iterGreater); // Output the k elements in the multiset
        }
         
        return result;
    }
};



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390276&siteId=291194637