题目:

使用栈实现队列的下列操作:

  • push(x) -- 将一个元素放入队列的尾部。
  • pop() -- 从队列首部移除元素。
  • peek() -- 返回队列首部的元素。
  • empty() -- 返回队列是否为空。

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

Notes:

  • You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

解题思路:

队列先进后出,栈后进先出。用栈实现队列,可以用两个栈完成题解。入队列时用 stack1 存入节点,出队列时 stack1 内节点顺序出栈压入 stack2 中。

例如 1, 2, 3 元素顺序入队列
即存入栈stack1:[1, 2, 3] 出队列时顺序应为:1->2->3
但是栈先进先出,出栈顺序为:3->2->1
与出队列顺序不相符 借助另一个栈stack2
stack1内的元素顺序出栈并压入stack2
stack1:[1, 2, 3] ---> stack2:[3, 2, 1]
此时stack2出栈顺序:1->2->3
与出队列顺序相符

注意:在出队列时无需着急将 stack1 中的节点顺序压入 stack2。因为要实现的队列是先进后出,可以将 stack2 中的节点全部弹出之后 再将 stack1 内节点顺序压入stack2,这样可以将出栈的时间复杂度摊还到 O(1)。

Java:

class MyQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
} public void push(int x) {
stack1.push(x);
} public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
} public int peek() {
//stack1节点顺序弹出并压入stack2
if (stack2.isEmpty()) {//条件是: stack2为空,而不是stack1非空, 这样摊还复杂度O(1)
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());//stack1弹出节点并压入stack2
}
}
return stack2.peek();
} public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}

Python:

Python语言没有栈和队列数据结构,只能用数组 List 或双端队列 deque 实现。

这类编程语言就压根不需要 用队列实现栈或用栈实现队列这种问题,因为栈和队列本身就必须借助List、deque实现。

所以这道题在这种语言中这就非常简单了,可以说是作弊。

class MyQueue:
def __init__(self):
self.queue = [] def push(self, x: int) -> None:
self.queue.append(x) def pop(self) -> int:
#弹出第一个元素
return self.queue.pop(0) def peek(self) -> int:
#返回第一个元素
return self.queue[0] def empty(self) -> bool:
return not self.queue

欢迎关注微.信公.众号:爱写Bug

LeetCode 232:用栈实现队列 Implement Queue using Stacks的更多相关文章

  1. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  2. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. Java实现 LeetCode 232 用栈实现队列

    232. 用栈实现队列 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返 ...

  4. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  5. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  6. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  7. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  8. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  9. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

随机推荐

  1. 《细说PHP》第四版 样章 第18章 数据库抽象层PDO 6

    18.5.3  PDO的错误处理模式 PDO共提供了3种不同的错误处理模式,不仅可以满足不同风格的编程,也可以调整扩展处理错误的方式. 1.PDO::ERRMODE_SILENT 这是默认模式,在错误 ...

  2. My97Datepicker 日历控件的使用

    如果显示中乱码可以再改变lang  js包 中的  以防乱码 var $lang = {errAlertMsg: "\u4E0D\u5408\u6CD5\u7684\u65E5\u671F\ ...

  3. kfifo

    kfifo 的一些伪代码 kfifo_len() out = LOAD fifo->out smp_rmb() len = LOAD fifo->in - out kfifo_in() k ...

  4. C#函数的参数传递方式1(值传递与地址传递)

    using System; namespace class1 { class program { static void Main(string[] args) { //值传递引用,实际参数不会变化 ...

  5. .net core项目启动时报_未处理Socket异常(以一种访问权限不允许的方式做了一个访问套接字的尝试。)

    解决方案:一般的原因就是程序的端口被占用了,关掉占用端口的程序即可正常使用.  查看启动地址和配置的webserver服务器的端口号是否被占用,可能占用的有:IIS启用项目,解决方案中其他启动项目配置 ...

  6. 使用CAD快速看图如何将图纸打印和预览?

    有相关CAD工作经验的小伙伴们都知道,绘制完CAD图纸后是需要借助CAD看图工具来进行查看图纸的,其实CAD快速看图中不仅能够对图纸进行查看,还能够将CAD图纸进行打印出来.但是有很多的伙伴不知道要怎 ...

  7. FCC---Animate Multiple Elements at Variable Rates---还可以改循环时间,达到不同律动频率的效果

    In the previous challenge, you changed the animation rates for two similarly animated elements by al ...

  8. xml解析-jaxp查询结点

    jaxp查询结点 eg://获取name的值 // person.xml <?xml version="1.0" encoding="UTF-8"?> ...

  9. ubuntu下查看本机IP地址

    在终端输入: ifconfig -a 即可得解.出来的结果inet后就是你的ip地址

  10. App iCON 尺寸

    120*120  180*180 58*58  87*87 80*80  120*120