当前位置:网站首页>20. 用两个栈实现队列
20. 用两个栈实现队列
2022-08-02 02:20:00 【Hunter_Kevin】
题目
请用栈实现一个队列,支持如下四种操作:
push(x) – 将元素x插到队尾;
pop() – 将队首的元素弹出,并返回该元素;
peek() – 返回队首元素;
empty() – 返回队列是否为空;
注意:
你只能使用栈的标准操作:push to top,peek/pop from top, size 和 is empty;
如果你选择的编程语言没有栈的标准库,你可以使用list或者deque等模拟栈的操作;
输入数据保证合法,例如,在队列为空时,不会进行pop或者peek等操作;
数据范围
每组数据操作命令数量 [0,100]。
样例
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
代码
维护两个栈,一个栈存储数据,另一个栈作为中转栈,满足队列的top、pop等操作
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> a, b;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
a.push(x);
}
void copy(stack<int> &x, stack<int> &y){
while(x.size()){
y.push(x.top());
x.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
copy(a,b);
int res = b.top();
b.pop();
copy(b,a);
return res;
}
/** Get the front element. */
int peek() {
copy(a,b);
int res = b.top();
copy(b,a);
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return a.empty();
}
};
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */
边栏推荐
猜你喜欢

From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

Chengdu openGauss user group recruit!

Scheduled tasks for distributed applications in Golang

The underlying data structure of Redis

Hash collisions and consistent hashing

Safety (2)
![[LeetCode Daily Question]——654. The largest binary tree](/img/05/0af1c6dc0085e253c0758c8da63e52.png)
[LeetCode Daily Question]——654. The largest binary tree

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
随机推荐
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
NIO's Sword
FOFAHUB使用测试
Safety (1)
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
Safety (2)
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
2022-08-01 反思
数据链路层的数据传输
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
通用客户端架构
AI目标分割能力,无需绿幕即可实现快速视频抠图
libcurl访问url保存为文件的简单示例
Use DBeaver for mysql data backup and recovery
CodeTon Round 2 D. Magical Array 规律
2022 Henan Youth Training League Game (3)