当前位置:网站首页>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(); */
边栏推荐
- MySQL - CRUD operations
- The failure to create a role in Dahua Westward Journey has been solved
- MySQL优化策略
- LeetCode刷题日记:LCP 03.机器人大冒险
- 项目后台技术Express
- swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
- Constructor instance method inheritance of typescript37-class (extends)
- Safety (2)
- [LeetCode Daily Question]——654. The largest binary tree
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
猜你喜欢
永磁同步电机36问(二)——机械量与电物理量如何转化?
Scheduled tasks for distributed applications in Golang
AOF rewrite
AI目标分割能力,无需绿幕即可实现快速视频抠图
MySQL optimization strategy
Safety (2)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
Use baidu EasyDL implement factory workers smoking behavior recognition
Remember a pit for gorm initialization
Garbage Collector CMS and G1
随机推荐
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
Data transfer at the data link layer
2022 Henan Youth Training League Game (3)
oracle查询扫描全表和走索引
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
考完PMP学什么?前方软考等着你~
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
【web】理解 Cookie 和 Session 机制
接口测试神器Apifox究竟有多香?
Personal blog system project test
Oracle19c安装图文教程
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
Analysis of the status quo of digital transformation of manufacturing enterprises
Redis Persistence - RDB and AOF
【web】Understanding Cookie and Session Mechanism
Speed up your programs with bitwise operations
2022-08-01 mysql/stoonedb慢SQL-Q18分析
Constructor instance method inheritance of typescript37-class (extends)
项目后台技术Express