当前位置:网站首页>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 emptyOperation 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
100Timepush、pop、peekandempty - Suppose all operations are valid ( for example , An empty queue will not call
popperhapspeekoperation )
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
边栏推荐
- LeetCode 1346、检查整数及其两倍数是否存在
- Browser - learning notes
- JDBC database addition, deletion, query and modification tool class
- LeetCode 542、01 矩阵
- selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
- Click an EL checkbox to select all questions
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- Recognition of high-speed road signs by Matlab using alexnet
- 三十.什么是vm和vc?
- Experience of mathematical modeling in 18 year research competition
猜你喜欢

Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
Handwritten Google's first generation distributed computing framework MapReduce

JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files

RT-1052学习笔记 - GPIO架构分析

Installation and use of NVM

How to protect ECs from hacker attacks?

SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in

Monte Carlo py solves the area problem! (save pupils Series)

Go zero framework database avoidance Guide

16MySQL之DCL 中 COMMIT和ROllBACK
随机推荐
Still using listview? Use animatedlist to make list elements move
Latest investigation and progress of building intelligence based on sati
Leetcode dynamic planning training camp (1-5 days)
考研英语唐叔的语法课笔记
【栈和队列专题】—— 滑动窗口
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
[latex] 5 how to quickly write out the latex formula corresponding to the formula
[PTA] l2-011 play with binary tree
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
2022 - Data Warehouse - [time dimension table] - year, week and holiday
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
Scripy tutorial - (2) write a simple crawler
A useless confession artifact
I JS deep copy and shallow copy
Installation and use of NVM
Markdown < a > tag new page open link
How do BIM swindlers cheat? (turn)
Vscode download speed up
黑客的入侵方式你知道几种?
C migration project record: modify namespace and folder name