注意 priority queue stack 都不是容器,而是容器适配器,所以其中是没有迭代器的,STL中的算法不适用于容器适配器
在默认情况下,优先级队列使用的是vector容器,less模版
优先级队列总是保证优先级最高的元素在队首,在使用less模版时,使用的是小于号
op(x,y)认为x小于y时,数学上x也小于y 因此 数学上最大的放在队首
//创建一个空的priority_queue容器适配器
std::priority_queue<int>values;
//使用 push() 成员函数向适配器中添加元素
values.push(3);//{3}
values.push(1);//{3,1}
values.push(4);//{4,1,3}
values.push(2);//{4,2,3,1}
//遍历整个容器适配器
while (!values.empty())
{
//输出第一个元素并移除。
std::cout << values.top()<<" ";
values.pop();//移除队头元素的同时,将剩余元素中优先级最大的移至队头
}
/* 函数对象类模板 */
template <typename T>
class cmp
{
public:
bool operator()(T a, T b)
{
return a > b;
}
};
/* 自定义优先级队列使用的底层容器与排序规则 */
priority_queue<int, vector<int>, cmp<int>> test;
test.push(1);
test.push(2);
test.push(3);
while(!test.empty())
{
cout << test.top() << endl;
test.pop();
}
头文件中的 std::less 和 std::greater ,各自底层实现采用的都是函数对象类模版的方式。
template <typename T>
struct less {
//定义新的排序规则
bool operator()(const T &_lhs, const T &_rhs) const {
return _lhs < _rhs;
}
};
template <typename T>
struct greater {
bool operator()(const T &_lhs, const T &_rhs) const {
return _lhs > _rhs;
}
};
注意 greater 和 less 是如何区分的:
STL中,在自定义了比较器op的情况下,以下三种说法是等价的
1.x
3.y大于x
重载 < >,其实是改变了 std::less 和 std::greater 的排序规则,从而间接实现了自定义排序
class node {
public:
node(int x = 0, int y = 0) :x(x), y(y) {}
int x, y;
};
//新的排序规则为:先按照 x 值排序,如果 x 相等,则按 y 的值排序
bool operator < (const node &a, const node &b)
{
if (a.x > b.x) return 1;
else if (a.x == b.x)
if (a.y >= b.y) return 1;
return 0;
}
还可以以成员函数的形式重载< 运算符
class node {
public:
node(int x = 0, int y = 0) :x(x), y(y) {}
int x, y;
bool operator < (const node &b) const{
if ((*this).x > b.x) return 1;
else if ((*this).x == b.x)
if ((*this).y >= b.y) return 1;
return 0;
}
};
以友元函数的形式重载< 或者 > 运算符时,要求参数必须使用 const 修饰。
class node {
public:
node(int x = 0, int y = 0) :x(x), y(y) {}
int x, y;
friend bool operator < (const node &a, const node &b);
};
//新的排序规则为:先按照 x 值排序,如果 x 相等,则按 y 的值排序
bool operator < (const node &a, const node &b){
if (a.x > b.x) return 1;
else if (a.x == b.x)
if (a.y >= b.y) return 1;
return 0;
}
/* 矩阵法来考虑动态规划问题 */
class Solution {
public:
bool isMatch(string s, string p) {
if (p.empty()) {
if (s.empty()) {
return true;
} else {
return false;
}
}
int m = s.length() + 1;
int n = p.length() + 1;
vector<vector<bool>> dp(m, vector<bool>(n, 0));
dp[0][0] = true;
for (int i = 1; i < n; i++) {
if ('*' == p[i - 1]) {
dp[0][i] = dp[0][i - 2];
}
}
for (int i = 1; i < m; i++) {
for (int k = 1; k < n; k++) {
if (p[k - 1] == '*') {
if (p[k - 2] == '.' || p[k - 2] == s[i - 1]) {
dp[i][k] = dp[i][k - 2] || dp[i-1][k];
} else {
dp[i][k] = dp[i][k - 2];
}
} else {
if (s[i - 1] == p[k - 1] || p[k - 1] == '.') {
dp[i][k] = dp[i - 1][k - 1];
}
}
}
}
return dp[m - 1][n - 1];
}
};
EIC2021刷题小分队 c d
冲刺037:反转链表 II(https://leetcode-cn.com/problems/reverse-linked-list-ii/)
c 冲刺038:二叉树的中序遍历(https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
冲刺039:验证二叉搜索树(https://leetcode-cn.com/problems/validate-binary-search-tree/)
c 冲刺034:x 的平方根(https://leetcode-cn.com/problems/sqrtx/)
c 冲刺035:爬楼梯(https://leetcode-cn.com/problems/climbing-stairs/)
c 冲刺036:最小覆盖子串(https://leetcode-cn.com/problems/minimum-window-substring/)
冲刺031:缺失的第一个正数(https://leetcode-cn.com/problems/first-missing-positive/)
冲刺032:全排列(https://leetcode-cn.com/problems/permutations/)
冲刺033:合并区间(https://leetcode-cn.com/problems/merge-intervals/)
冲刺028:两数相加(https://leetcode-cn.com/problems/add-two-numbers/)
冲刺029:最长回文子串(https://leetcode-cn.com/problems/longest-palindromic-substring/)
冲刺030:组合总和(https://leetcode-cn.com/problems/combination-sum/)
冲刺025:最长递增子序列(https://leetcode-cn.com/problems/longest-increasing-subsequence/)
冲刺026:字符串相加(https://leetcode-cn.com/problems/add-strings/)
冲刺027:合并K个升序链表(https://leetcode-cn.com/problems/merge-k-sorted-lists/)
冲刺022:反转链表(https://leetcode-cn.com/problems/reverse-linked-list/)
冲刺023:数组中的第K个最大元素(https://leetcode-cn.com/problems/kth-largest-element-in-an-array/)
冲刺024:二叉树的最近公共祖先(https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
冲刺019:相交链表(https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
冲刺020:二叉树的右视图(https://leetcode-cn.com/problems/binary-tree-right-side-view/)
冲刺021:岛屿数量(https://leetcode-cn.com/problems/number-of-islands/)
d冲刺016:买卖股票的最佳时机(https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
d冲刺017:环形链表(https://leetcode-cn.com/problems/linked-list-cycle/)
d冲刺018:LRU 缓存机制(https://leetcode-cn.com/problems/lru-cache/)
d冲刺013:二叉树的层序遍历(https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
d 冲刺014:二叉树的锯齿形层序遍历(https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/)
冲刺015:从前序与中序遍历序列构造二叉树(https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
冲刺010:接雨水(https://leetcode-cn.com/problems/trapping-rain-water/)
冲刺011:最大子序和(https://leetcode-cn.com/problems/maximum-subarray/)
冲刺012:合并两个有序数组(https://leetcode-cn.com/problems/merge-sorted-array/)
d 冲刺007:搜索旋转排序数组(https://leetcode-cn.com/problems/search-in-rotated-sorted-array/)
冲刺008:合并两个有序链表(https://leetcode-cn.com/problems/merge-two-sorted-lists/)
冲刺009:下一个排列(https://leetcode-cn.com/problems/next-permutation/)
d 冲刺004:两数之和(https://leetcode-cn.com/problems/two-sum/)
冲刺005:三数之和(https://leetcode-cn.com/problems/3sum/)
冲刺006:螺旋矩阵(https://leetcode-cn.com/problems/spiral-matrix/)
冲刺001:无重复字符的最长子串(https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ )
冲刺002:K 个一组翻转链表(https://leetcode-cn.com/problems/reverse-nodes-in-k-group/ )
冲刺003:有效的括号(https://leetcode-cn.com/problems/valid-parentheses/ )