leetcode 347 前k个高频元素

简介: leetcode 347 前k个高频元素

前k个高频元素


2bb955e991ea4f309d228a2844ac276e.png


vector排序法

class Solution {
public:
  //对vector排序的谓词,前面加static
    static bool compare(pair<int, int> map1, pair<int, int> map2) 
    {
        return map1.second > map2.second;
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> num_map; //map统计出现的次数
        vector<pair<int ,int >> buf; //缓存vector,用作排序
        vector<int> result;
        for( auto i:nums)
        {
            num_map[i]++; //统计出现的次数
        }
        for ( auto it : num_map) //将map中的数据存到vector中,用pair的形式
        {
            buf.push_back(make_pair(it.first, it.second)); 
            // cout<<it.first<<' '<<it.second<<endl;
        }
        sort(buf.begin(), buf.end(),compare);    //排序,按照value的大小排序
        for(int i = 0 ; i<k ;i++)
        {
            result.push_back(buf[i].first);   //前k个值为结果
        }
        return result;
    }
};

小顶堆

class Solution {
public:
    // 小顶堆的比较函数
    class mycomparison {
    public:
        bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
            return lhs.second > rhs.second;
        }
    };
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> num_map;
        vector<int> result;
        // 对频率排序
        // 定义一个小顶堆,大小为k
        priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison>  pri_que;
        for( auto i:nums)
        {
            num_map[i]++;
        }
        for ( auto it : num_map)
        {
            pri_que.push(it);
            if (pri_que.size() > k)// 如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
            { 
                pri_que.pop();
            }
        }
        for(int i = k - 1; i >= 0; i--) //小顶堆,先出的小,倒着装入数组
        {
            result.push_back( pri_que.top().first);
            pri_que.pop();
        }
        return result;
    }
};

二刷

map排序

class Solution {
public:
    static bool cmp(const pair<int,int>&a, const pair<int,int>&b )
    {
        return a.second > b.second;
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> my_map;
        vector<int> resulte;
        for(int i=0 ; i<nums.size() ;i++)
        {
            my_map[nums[i]]++;
        }
        vector<pair<int,int>> tmp(my_map.begin(),my_map.end()); 
        sort(tmp.begin(),tmp.end(),cmp);
        for(int i=0 ; i<k ;i++)
        {
            resulte.push_back(tmp[i].first);
        }
        return resulte;
    }
};
相关文章
|
1月前
|
算法 Java
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
23 1
|
1月前
|
算法 C语言
【C语言】Leetcode 27.移除元素
【C语言】Leetcode 27.移除元素
21 0
【C语言】Leetcode 27.移除元素
|
1月前
|
C++
两种解法解决 LeetCode 27. 移除元素【C++】
两种解法解决 LeetCode 27. 移除元素【C++】
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
1月前
|
算法
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
16 1
|
3月前
leetcode:203. 移除链表元素(有哨兵位的单链表和无哨兵位的单链表)
leetcode:203. 移除链表元素(有哨兵位的单链表和无哨兵位的单链表)
19 0
|
5天前
|
算法
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
9 3
|
8天前
|
算法
【力扣】169. 多数元素
【力扣】169. 多数元素
|
1月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
1月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0

热门文章

最新文章