当前位置:网站首页>232. Implement queue with stack
232. Implement queue with stack
2022-07-19 15:29:00 【yitahutu79】
Please use only two stacks to implement the FIFO queue . The queue should support all operations supported by the general queue (push、pop、peek、empty):
Realization MyQueue class :
void push(int x) Put the element x Push to the end of the queue
int pop() Remove from the beginning of the queue and return the element
int peek() Returns the element at the beginning of the queue
boolean empty() If the queue is empty , return true ; otherwise , return false
explain :
you Can only Use standard stack operations —— It's just push to top, peek/pop from top, size, and is empty Operation is legal .
The language you use may not support stacks . You can use list perhaps deque( deque ) To simulate a stack , As long as it's a standard stack operation .
Example 1:
Input :
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output :
[null, null, null, 1, 1, false]
explain :
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Tips :
1 <= x <= 9
Call at most 100 Time push、pop、peek and empty
Suppose all operations are valid ( for example , An empty queue will not call pop perhaps peek operation )
Advanced :
Can you realize the time sharing of each operation? The complexity is O(1) Queues ? let me put it another way , perform n The total time complexity of operations is O(n) , Even if one of the operations may take a long time .
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
MyQueue() {
}
void push(int x) {
stIn.push(x);
}
int pop() {
if (stOut.empty()){
while (!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int front = stOut.top();
stOut.pop();
return front;
}
int peek() {
if (stOut.empty()){
while (!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int front = stOut.top();
return front;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
/** * Your MyQueue object will be instantiated and called as such: * MyQueue* obj = new MyQueue(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->peek(); * bool param_4 = obj->empty(); */

边栏推荐
- Codeforces round 807 (Div. 2) e. mark and Professor Koro binary / segment tree
- Wechat applet 6 cloud development cloud database
- Stack和Queue容器的系列操作( 详解 )
- 2020 ICPC Asia East continuous final g. Prof. Pang's sequence line segment tree / scan line
- 【软件测试】——postman接口测试工具完整教程
- ArkUI常见问题汇总【系列2】
- C# FTP双网卡问题
- 股票财务信息,董事会,监事会等高管信息爬取
- Which securities company should I choose to open a stock account? What securities company is safer
- How should I design my blog? How to build a blog with good experience?
猜你喜欢

Wechat applet 8 cloud function

OSPF学习笔记(四)
![[code attached] how to realize handwritten digit recognition with hog+svm](/img/e1/870aec8bb75c7cf82dee62c68d1009.jpg)
[code attached] how to realize handwritten digit recognition with hog+svm

DevOps工具链:开放、自由地选择最适合团队和业务需要的工具
![[software testing] - a complete tutorial of postman interface testing tool](/img/4b/e8ee6fdf87d5b57adb8d3b6804c3ec.png)
[software testing] - a complete tutorial of postman interface testing tool
![2040: [蓝桥杯2022初赛] 砍竹子(优先队列)](/img/76/512b7fd4db55f9f7d8f5bcb646d9fc.jpg)
2040: [蓝桥杯2022初赛] 砍竹子(优先队列)

Li Hongyi machine learning 2022.7.15 -- gradient descent

智牛股--08

C# FTP双网卡问题

用对工具,CI事半功倍
随机推荐
Arkui route jump
Is it safe to open an account and buy funds on the Internet? Just contacted the fund and didn't know how to ask for guidance
ARM系统调用异常 汇编
原始套接字
DevOps工具链:开放、自由地选择最适合团队和业务需要的工具
vscod
OSPF learning notes (4)
OSPF学习笔记(三)
[LeetCode]-动态规划-4
PostgreSQL installation et démarrage des tutoriels de base sous Linux et Windows
Is it safe for Hengtai securities to open an account online?
I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)
ssh 使用 socks5 代理连接到远端服务器中
Impact analysis: rubygems unauthorized access vulnerability (cve-2022-29176)
2022-7-17
CarMaker快速入门第四课开发48V P1混动系统
Using flex layout to realize local scroll bar
Selenium元素操作
ADN公益加速 —— Jsdelivr npm(国内),ElmCDN的优质替代方案
LBP特征笔记