LeetCode刷题--栈,队列,堆
程序员文章站
2022-06-07 21:47:08
...
1.栈、队列、堆(优先队列)的基础
STL中的容器适配器有stack, queue, priority_queue三种,它们都是在顺序容器的基础上实现,屏蔽了顺序容器的一部分功能,突出和增加了另外一些功能。容器适配器都有以下三个成员函数:
this->push():添加一个元素
this->top():返回顶部(stack) 或 队头(queue,priority_queue)的元素的引用
this->pop():删除一个元素
2.栈和队列的基本操作
2.1 利用队列实现栈
#include<queue>
class mystack
{
private:
queue<int> _data;
public:
mystack() {};
void push(int x)
{
queue<int> temp_data;
temp_data.push(x);
while(!_data.empty())
{
temp_data.push(_data.front());
_data.pop();
}
while(!temp_data.empty())
{
_data.push(temp_data.front());
temp_data.pop();
}
}
void pop()
{
this->_data.pop();
}
int& top()
{
return this->_data.front();
}
bool empty()
{
return this->_data.empty();
}
int size()
{
return this->_data.size();
}
};
2.2 利用栈实现队列
分析过程与上面类似
#include<stack>
class myqueue
{
private:
stack<int> _data;
public:
myqueue() {};
void push(int x)
{
stack<int> temp_data;
while(!_data.empty())
{
temp_data.push(_data.top());
_data.pop();
}
temp_data.push(x);
while(!temp_data.empty())
{
_data.push(temp_data.front());
temp_data.pop();
}
}
void pop()
{
this->_data.pop();
}
int& top()
{
return this->_data.top();
}
bool empty()
{
return this->_data.empty();
}
int size()
{
return this->_data.size();
}
};
3.栈的混合使用
3.1 包含min函数的栈
#include<stack>
class minStack
{
private:
stack<int> _data;
stack<int> _min;
public:
void push(int x)
{
_data.push(x);
if(_min.empty()) _min.push(x);
else _min.push( x<_min.top()? x:_min.top());
}
void pop()
{
_data.pop();
_min.pop();
}
int top()
{
return _data.top();
}
int get_min()
{
return _min.top();
}
}
4.栈和队列的混合使用
4.1 合法的出栈序列
#include<stack>
#include<queue>
bool check_is_valid_order(std::queue<int> &order)
{
std::stack<int> s;
int n=order.size();
for(int i=1; i<=n; ++i)
{
s.push(i);
while(!s.empty() && order.front()==s.top())
{
s.pop();
order.pop();
}
}
if s.empty()?return true : return false;
}
4.2 简答的计算器
利用工业采用的“有限状态自动机”来进行coding,还未完成代码编辑工作
#include<stack>
#include<string>
void compute(stack<int>&number_stack, stack<char>operator_stack)
{
if(number_stack.size()<2) return;
int num2 = number_stack.top(); number_stack.pop();
int num1 = number_stack.top(); number_stack.pop();
if(operator_stack.top()=='+') number_stack.push(num1+num2);
else if(operator_stack.top()=='-') number_stack.push(num1-num2);
operator_stack.pop();
}
int calculate(string s)
{
static const int STATE_BEGIN = 0;
static const int STATE_NUMBER = 1;
static const int STATE_OPERATOR = 2;
stack<int> number_stack;
stack<char> operator_stack;
int number = 0;
int STATE = STATE_BEGIN;
int compute_flag = 0;
for(int i=0; i<s.size(); ++i)
{
if(s[i]==' ') continue;
switch(STATE)
case STATE_BEGIN:
if(s[i]>='0 && s[i]<='9')
STATE = STATE_NUMBER;
else
STATE = STATE_OPERATOR;
--i;
break;
case STATE
}
}
5. STL优先队列(二叉堆)
STL中最大堆和最小堆的基础知识:
默认为最大堆: priority_queue max_pq;
更改配置为最小堆:priority_queue<double,vector,greater> min_pq;
5.1 利用堆维护数组中最大或最小的前k个数
#include<queue>
#include<vector>
int findKthLargest(vector<int>& nums, int k)
{
priority_queue<int, vector<int>, greater<int>> min_pq;
for(int i=0; i<nums.size(); ++i)
{
if(min_pq.size<k) min_pq.push(nums[i]);
else if(min_pq.top()<nums[i])
{
min_pq.pop();
min_pq.push(nums[i]);
}
}
return min_pq.top();
}
5.2 利用堆来获取中位数
#include<queue>
priority_queue<int> small_pq;
priority_queue<int,vector<int>,greator<int>> big_pq;
void addNum(int num)
{
if(big_pq.empty())
{
big_pq.push(num);
return ;
}
if(big_pq.size()==small_pq.size()) //情形1
{
num<big_pq.top()? big_pq.push(num):samll_pq.push(num);
}
else if(big_pq.size()>samll_pq.size()) //情形2
{
if(num>big_pq.top()) samll_pq.push(num);
else
{
samll_pq.push(big_pq.top());
big_pq.pop();
big_pq.push(num);
}
}
else if(big_pq.size()<samll_pq.size()) //情形3
{
if(num<small_pq.top()) big_pq.push(num);
else
{
big_pq.push(small_pq.top());
small_pq.pop();
small_pq.push(num);
}
}
}
double findMedian()
{
if(big_pq.size() == small_pq.size())
return (big_pq.top()+samll_pq.top()) / 2.0;
else if(big_pq.size<small_pq.size())
return small_pq.top();
else return big_pq.top();
}
6.本模块总结
- 对于容器适配器的使用目的是为了优化时间复杂度;
- 在coding的过程中,要从中间思维,不要从开始去想;
- 还需要刷题和看慕课视频。
上一篇: AMD RX 500系显卡型号及参数全曝光:RX570性价比最高
下一篇: HDU-2588-GCD