• 代码题(3)— 最小的k个数、数组中的第K个最大元素、前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、215、数组中的第K个最大元素

    题目:在未排序的数组中找到第 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 if(low<high && index > k-1)
                {
                    high = index-1;
                    index = partition(nums, low,high);
                    
                }
                else
                    break;
            }
            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;
        }
    };

     3、347. 前K个高频元素

    给定一个非空的整数数组,返回其中出现频率前 高的元素。

    示例 1:

    输入: nums = [1,1,1,2,2,3], k = 2
    输出: [1,2]
    

    示例 2:

    输入: nums = [1], k = 1
    输出: [1]

    说明:

    • 你可以假设给定的 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
    • 你的算法的时间复杂度必须优于 O(n log n) , 是数组的大小。
    class Solution {
    public:
        vector<int> topKFrequent(vector<int>& nums, int k) {
            unordered_map<int, int> m;
            priority_queue<pair<int,int>> q;
            vector<int> res;
            
            for(int a:nums)
                m[a]++;
            for(auto it:m)
                q.push({it.second, it.first});
            for(int i=0;i<k;++i)
            {
                res.push_back(q.top().second);
                q.pop();
            }
            return res;
        }
    };
  • 相关阅读:
    空悬指针、野指针、内存泄漏、内存溢出
    自定义消息的操作方法ON_MESSAGE(..)
    为什么static成员变量一定要在类外初始化?
    Ubuntu 系统目录结构
    Beyond Compare 4 30天评估期结束的解决办法
    C++ string 字符串 结尾 标志
    C语言——枚举类型用法
    结构体struct-联合体union-枚举enum
    网卡bood
    kvm 安装
  • 原文地址:https://www.cnblogs.com/eilearn/p/9206058.html
Copyright © 2020-2023  润新知