代码题(3)— 最小的k个数

  1、题目:输入n个整数,找出其中最小的K个数。
  例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。

  快排思路(掌握):

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        vector<int> result;
        if(input.empty() || k<=0 || input.size()<k)
            return result;
        int low = 0;
        int high = input.size() - 1;
        int index = partition(input, low, high);
        while(index != k-1)
        {
            if(index <= k-1 )
            {
                low = index + 1;
                index = partition(input, low, high);
            }
            else 
            {
                high = index - 1;
                index = partition(input, low, high);
            }
        }
        for(int i =0; i<k; ++i)
        {
            result.push_back(input[i]);
        }
        return result;
    }
    int partition(vector<int> &input, int low, int high)
    {
        int temp = input[low];
        while(low < high)
        {
            while(low<high && temp < input[high])
                high--;
            input[low] = input[high];
            while(low<high && temp >=input[low])
                low++;
            input[high] = input[low];
        }
        input[low] = temp;
        return low;
    }
};

  使用容器的方法:

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        vector<int> result;
        if(input.empty() || k<1 || input.size()<k)
            return result;
        priority_queue<int> pq;
        for(int i=0;i<input.size();i++)
        {
            if(pq.size()<k)
            {
                pq.push(input[i]);
            }
            else
            {
                int maxVal=pq.top();
                if(input[i]<maxVal)
                {
                    pq.pop();
                    pq.push(input[i]);
                }
            }
        }
        while(!pq.empty())
        {
            result.push_back(pq.top());
            pq.pop();
        }
        return result;
        
    }
};

2、题目:在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入: [3,2,1,5,6,4] 和 k = 2
输出: 5

示例 2:

输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        if(nums.empty() || nums.size()<k || k<=0)
            return -1;
        vector<int> copyNums = nums;
        int low = 0;
        int high = nums.size()-1;
        int index = partition(nums, low, high);
        while(index != k-1)
        {
            
            if(low<high && index <k-1)
            {
                low = index+1;
                index = partition(nums,low, high);
            }
            else
            {
                high = index-1;
                index = partition(nums, low,high);
                
            }
        }
        for(int i=0;i<nums.size();++i)
        {
            if(copyNums[i] == nums[index])
                return copyNums[i];
        }
        return -1;
        
    }
    int partition(vector<int> &input, int low, int high)
    {
        int temp = input[low];
        while(low < high)
        {
            while(low<high && temp > input[high])
                high--;
            input[low] = input[high];
            while(low<high && temp <=input[low])
                low++;
            input[high] = input[low];
        }
        input[low] = temp;
        return low;
    }
};

猜你喜欢

转载自www.cnblogs.com/eilearn/p/9206058.html