当前位置:网站首页>栈与队列——232. 用栈实现队列
栈与队列——232. 用栈实现队列
2022-07-24 13:53:00 【向着百万年薪努力的小赵】
1 题目描述
- 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
2 题目示例
输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
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
3 题目提示
1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
4 思路
队列是—种先进先出(first in - first out,FIFO)的数据结构,队列中的元素都从后端(rear)入队(push),从前端(front)出队(pop)。
实现队列最直观的方法是用链表,但在这篇文章里我会介绍另—个方法-使用栈。
栈是一种后进先出(last in - first out,LIFO)的数据结构,栈中元素从栈顶(top)压入(push),也从栈顶弹出(pop)。为了满足队列的FIFO的特性,我们需要用到两个栈,用它们其中一个来反转元素的入队顺序,用另一个来存储元素的最终顺序。
入队(push)
一个队列是FIFO的,但一个栈是 LIFO的。这就意味着最新压入的元素必须得放在栈底。为了实现这个目的,我们首先需要把s1中所有的元素移到s2中,接着把新元素压入s2。最后把s2中所有的元素弹出,再把弹出的元素压入s1。
出队(pop)
直接从s1弹出就可以了,因为s1的栈顶元素就是队列的队首元素。同时我们把弹出之后s1的栈顶元素赋值给代表队首元素的front变量。
判断空(empty)
s1存储了队列所有的元素,所以只需要检查s1 的是否为空就可以了。
取队首元素(peek)
在我们的算法中,用了front变量来存储队首元素,在每次入队操作或者出队操作之后这个变量都会随之更新。
5 我的答案
入队
private int front;
public void push(int x) {
if (s1.empty())
front = x;
while (!s1.isEmpty())
s2.push(s1.pop());
s2.push(x);
while (!s2.isEmpty())
s1.push(s2.pop());
}
出队
// Removes the element from the front of queue.
public void pop() {
s1.pop();
if (!s1.empty())
front = s1.peek();
}
判空
// Return whether the queue is empty.
public boolean empty() {
return s1.isEmpty();
}
取队首元素
// Get the front element.
public int peek() {
return front;
}
边栏推荐
猜你喜欢

网络安全——文件上传竞争条件绕过

网络安全——Web渗透测试

【无标题】

网络安全——文件上传内容检查绕过

使用树莓派做Apache2 HA实验

Network security - Web penetration testing

网络安全——使用Evil Maid物理访问安全漏洞进行渗透

网络安全——文件上传白名单绕过

Paper notes: swing UNET: UNET like pure transformer for medicalimage segmentation

Group knowledge map: distributed knowledge transfer and federated map reasoning
随机推荐
Network security - Web penetration testing
CSDN garbage has no bottom line!
Flink fault tolerance mechanism (V)
使用树莓派做Apache2 HA实验
R language test sample proportion: use the prop.test function to perform a single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
网络安全——文件上传白名单绕过
position: -webkit-sticky; /* for Safari */ position: sticky;
网络安全——使用Exchange SSRF 漏洞结合NTLM中继进行渗透测试
Lazy loading of pictures
The latest and complete Flink series tutorials in 2021_ Preliminary exploration of Flink principle and flow batch integration API (II. V) V2
The R language uses the sort function to sort vector data and return the actually sorted data (ascending by default)
Flink综合案例(九)
Noip2021 T2 series
Uni app background audio will not be played after the screen is turned off or returned to the desktop
Csp2021 T1 corridor bridge distribution
R语言tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数通过列索引指定不变的列名称列表
通配符(泛域名)SSL证书
软链接、硬链接
Flink综合案例(九)
网络安全——文件上传内容检查绕过