优先队列(priority_queue)用法:
相同处:
同queue,都需要使用 #include
;
和队列的基本操作相同
1、top访问队头元素
2、pop弹出队头元素
3、empty队列是否为空
4、push插入元素到队尾(并排序)
5、size返回队列内元素的个数
6、emplace原地构造一个元素并插入队列
7、swap内容交换
不同处:
”披着队列外衣“的堆,添加了内部排序
优先队列(priority_queue)定义:
priority_queue
Type是数据类型,Container是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用list。默认的类型是vector),Functional就是比较方式,默认是大顶堆
代码:
//升序队列
priority_queue,greater>q;
//降序队列
priority_queue,less>q;
//greater和less是std的两个仿函数(就是使用一个类看上去是一个函数。其实就是在类中实现了一个operator(),这个类就有了类似函数的行为,就是模仿函数了)
基本用法代码:
#include
#include
using namespace std;
int main(){
//默认就是大顶堆
priority_queuea;//相当于priority_queue,less >a;
//小顶堆 (升序)
priority_queue,greater >c;
// 大顶堆
priority_queueb;
for(int i=4;i>=0;i--){
a.push(i);
c.push(i);
}
while(!a.empty()){
cout << a.top() << ' ';
a.pop();
}
cout << endl;
while(!c.empty()){
cout << c.top() << ' ';
c.pop();
}
cout << endl;
b.push("abc");
b.push("abcd");
b.push("bcd");
while(!b.empty()){
cout << b.top() << ' ';
b.pop();
}
cout << endl;
return 0;
}
输出:
4 3 2 1 0
0 1 2 3 4
cbd abcd abc
pair的比较,先比较第一个元素,第一个相等比较第二个
#include
#include
#include
using namespace std;
int main(){
priority_queue >a;
pairb(2,5);
pairc(1,2);
paird(1,3);
a.push(b);
a.push(c);
a.push(d);
while(!a.empty()){
cout << a.top().first << ' ' <
输出:
2 5
1 3
1 2
自定义类型
#include
#include
using namespace std;
struct tmp1{
int x;
tmp1(int a){
x=a;
}
bool operator<(const tmp1& a)const{
return x d;
d.push(a);
d.push(b);
d.push(c);
while(!d.empty()){
cout << d.top().x << '\n';
d.pop();
}
cout << endl;
priority_queue,tmp2> f;
f.push(a);
f.push(b);
f.push(c);
while(!f.empty()){
cout << f.top().x << '\n';
f.pop();
}
cout << endl;
return 0;
}
给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:
输入: nums = [1], k = 1
输出: [1]
提示:
1 <= nums.length <= 105
k 的取值范围是 [1, 数组中不相同的元素的个数]
题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的
进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。
(代码时间复杂度为n*logk):
class Solution {
public:
class mycomparison{
public:
bool operator()(const pair& lhs,const pair& rhs){
return lhs.second>rhs.second;
}
};
vector topKFrequent(vector& nums, int k) {
unordered_mapmap;
for(int i=0;i,vector>,mycomparison> pri_que;
for(unordered_map::iterator it=map.begin();it!=map.end();it++){
pri_que.push(*it);
if(pri_que.size()>k){
pri_que.pop();
}
}
vectorresult(k);
for(int i=k-1;i>=0;i--){
result[i]=pri_que.top().first;
pri_que.pop();
}
return result;
}
};
*一直在跟着卡神刷力扣,这篇真的费了好大劲才刷下来,内容上加了自己的话,在这里鼓励一下自己,继续加油!!!