当前位置:网站首页>Leetcode 232, queue with stack
Leetcode 232, queue with stack
2022-04-23 20:26:00 【Die in a trap】
232、 Using stack to realize queue
1) Title Description
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 queueint pop()
Remove from the beginning of the queue and return the elementint peek()
Returns the element at the beginning of the queueboolean empty()
If the queue is empty , returntrue
; otherwise , returnfalse
explain :
- you Can only Use standard stack operations —— It's just
push to top
,peek/pop from top
,size
, andis 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
Timepush
、pop
、peek
andempty
- Suppose all operations are valid ( for example , An empty queue will not call
pop
perhapspeek
operation )
2) analysis
Because the stack is first in and last out , The queue is first in, first out , Two stacks can be used to exchange the order .
3)C++
Code
class MyQueue {
public:
stack<int> s1;
stack<int> s2;
MyQueue() {
}
void push(int x) {
s1.push(x);
}
int pop() {
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
int res=s2.top();
s2.pop();
return res;
}
int peek() {
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
int res=s2.top();
return res;
}
bool empty() {
return s1.empty()&&s2.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(); */
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107405.html
边栏推荐
- Recognition of high-speed road signs by Matlab using alexnet
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- Use the rolling division method to find the maximum common divisor of two numbers
- Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
- 【栈和队列专题】—— 滑动窗口
- LeetCode 20、有效的括号
- 堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
- Commit and rollback in DCL of 16 MySQL
- DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
- Notes of Tang Shu's grammar class in postgraduate entrance examination English
猜你喜欢
[latex] 5 how to quickly write out the latex formula corresponding to the formula
BMP JPEG picture to vector image contourtrace
[PTA] l1-002 printing hourglass
Click an EL checkbox to select all questions
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
【PTA】整除光棍
16MySQL之DCL 中 COMMIT和ROllBACK
PCL点云处理之计算两平面交线(五十一)
Numpy mathematical function & logical function
随机推荐
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
PCL点云处理之基于PCA的几何形状特征计算(五十二)
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
Thirty What are VM and VC?
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
【PTA】L2-011 玩转二叉树
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Latest investigation and progress of building intelligence based on sati
LeetCode 542、01 矩阵
After route link navigation, the sub page does not display the navigation style problem
Sqoop imports tinyint type fields to boolean type
2022 - Data Warehouse - [time dimension table] - year, week and holiday
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Es error: request contains unrecognized parameter [ignore_throttled]
Customize timeline component styles
LeetCode 74、搜索二维矩阵
Computing the intersection of two planes in PCL point cloud processing (51)
How do BIM swindlers cheat? (turn)
Confusion about thread blocking after calling the read () method of wrapper flow
Intersection calculation of straight line and plane in PCL point cloud processing (53)