priority_que,unordered_map等具体使用情况

priority_que,unordered_map等具体使用情况

LeetCode 347. Top K Frequent Elements

题目描述

Given a non-empty array of integers, return the k most frequent elements.

示例说明:

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:

Input: nums = [1], k = 1
Output: [1]
Note:

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

题目分析:

该题采用unorder_map(底层用hash表实现)进行频率的统计,后面用priority_queue(小顶堆)进行排序统计,最后输出小顶堆里面的k个元素即可。

实现代码:

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        vector<int>res;
        if(nums.size()<=0||k>nums.size())
            return res;
        
        //hash_map<int,int>freq;
        unordered_map<int,int>freq;
        for(int i=0;i<nums.size();i++)
            freq[nums[i]]++;
        
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;  //用小顶堆
        
        for(unordered_map<int,int>::iterator it=freq.begin();it!=freq.end();it++)
        {
            if(pq.size()==k)
            {
                if(pq.top().first<it->second)
                {
                    pq.pop();
                    pq.push(make_pair(it->second,it->first));
                }            
            }
                
            else
                pq.push(make_pair(it->second,it->first));
        }
        
        while(!pq.empty())
        {
            res.push_back(pq.top().second);
            pq.pop();
        }
        
        return res;
    }
};

LeetCode 23. Merge k Sorted Lists

题目描述:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

示例说明:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

题目分析:

该题目适合用小顶堆进行求解,但是要注意priority_queue中默认的是大顶堆(less),自己需要根据实际情况进行定义greater(小顶堆),同时需要根据实际情况进行定义底层的数据形式。

实现代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        
        ListNode* dummy=new ListNode(0);
        ListNode* p=dummy;
        
        priority_queue<pair<int,ListNode*>,vector<pair<int,ListNode*>>,greater<pair<int,ListNode*>>>pq;  //greater是小顶堆!!!
        
        for(int i=0;i<lists.size();i++)
        {
            if(lists[i]!=NULL)
                pq.push(make_pair(lists[i]->val,lists[i]));  
        }
        
        while(!pq.empty())
        {
            ListNode* cur=pq.top().second;
            pq.pop();
            p->next=cur;
            p=p->next;
            if(cur->next!=NULL)
                pq.push(make_pair(cur->next->val,cur->next));    
        }
        p->next=NULL;
        ListNode* res=dummy->next;
        delete dummy;
        return res; 
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值