当前位置:网站首页>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
边栏推荐
- 一. js的深拷贝和浅拷贝
- I JS deep copy and shallow copy
- Servlet learning notes
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
- Investigate why close is required after sqlsession is used in mybatties
- Numpy Index & slice & iteration
- R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
- Three. Based on ply format point cloud voxel model JS upload interface writing
- R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
猜你喜欢
go-zero框架数据库方面避坑指南
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Handwritten Google's first generation distributed computing framework MapReduce
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Three. Based on ply format point cloud voxel model JS upload interface writing
[PTA] l1-002 printing hourglass
随机推荐
ArcGIS js api 4. X submergence analysis and water submergence analysis
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Research on open source OCR engine
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Numpy - creation of data type and array
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
go-zero框架数据库方面避坑指南
How do BIM swindlers cheat? (turn)
Numpy mathematical function & logical function
Scripy tutorial - (2) write a simple crawler
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Numpy Index & slice & iteration
How does onlyoffice solve no route to host
Solve the Chinese garbled code of URL in JS - decoding
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
LeetCode 994、腐烂的橘子
Some basic configurations in interlij idea
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in