当前位置:网站首页>232. Implement queue with stack (simple)
232. Implement queue with stack (simple)
2022-07-22 06:40:00 【Heavy garbage】
Train of thought : Dual stack simulation
Two stacks :stk1 stk2
The team : Push stk1
Out of the team : If stk2 It's empty , Then put stk1 All out of stack and in stack stk2, And then out of the team
Team head : That is to say stk2 Top of stack , If stk2 If it is empty, the stk1 All out of stack and in stack stk2, Then take the head of the team
It's empty : stk1 stk2 All empty.
class MyQueue {
public:
stack<int> stk1, stk2;
MyQueue() {
}
void push(int x) {
stk1.push(x);
}
int pop() {
if (stk2.empty()) {
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
int tp = stk2.top();
stk2.pop();
return tp;
}
int peek() {
if (stk2.empty()) {
while (!stk1.empty()) {
stk2.push(stk1.top());
stk1.pop();
}
}
return stk2.top();
}
bool empty() {
return stk1.empty() && stk2.empty();
}
};
边栏推荐
猜你喜欢
随机推荐
How many tricks do you know about killing oom!
void 0 有什么意义?undefined竟然是可变的?
Xshell远程连接服务器
Node项目中使用jsonwebtoken实现JWT认证
酷玩周刊 第 10 期
CAD对象的夹点被编辑完成后调用事件(com接口VB语言)
设置MySQL时区
同城订单同城送,爆单依旧得心应手!
Dom4j parsing XML (detailed)
UE4 学习笔记 CheckBox替代button + image
Preparation of chitosan / dextran / nano hydroxyapatite composite hydrogel / fish gelatin galactose chitosan hydrogel liver scaffold
U++ UPROPERTY UFUNCTION 基础
OSPF实验演示(Huawei路由器设备配置)
吉时利Keithley软件2400|2440|2450|2460|2461|2470 NS-SourceMeter源表软件
Xiaodaoxian blog [open source personal blog]
Leetcode skimming: bit operation (find numbers that are different and appear only once)
Exploration of running applet of domestic Tongxin UOS system
LeetCode刷题:对称二叉树与二叉树的最大深度
Node实现批量修改文件名(文件重命名)
idea 改变字体大小