题目:

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

  • 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.

示例:

  1. MyQueue queue = new MyQueue();
  2. queue.push(1);
  3. queue.push(2);
  4. queue.peek(); // 返回 1
  5. queue.pop(); // 返回 1
  6. 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. 例如 1, 2, 3 元素顺序入队列
  2. 即存入栈stack1:[1, 2, 3]
  3. 出队列时顺序应为:1->2->3
  4. 但是栈先进先出,出栈顺序为:3->2->1
  5. 与出队列顺序不相符
  6. 借助另一个栈stack2
  7. stack1内的元素顺序出栈并压入stack2
  8. stack1:[1, 2, 3] ---> stack2:[3, 2, 1]
  9. 此时stack2出栈顺序:1->2->3
  10. 与出队列顺序相符

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

Java:

  1. class MyQueue {
  2. private Stack<Integer> stack1;
  3. private Stack<Integer> stack2;
  4. public MyQueue() {
  5. stack1 = new Stack<>();
  6. stack2 = new Stack<>();
  7. }
  8. public void push(int x) {
  9. stack1.push(x);
  10. }
  11. public int pop() {
  12. if (stack2.isEmpty()) {
  13. while (!stack1.isEmpty()) {
  14. stack2.push(stack1.pop());
  15. }
  16. }
  17. return stack2.pop();
  18. }
  19. public int peek() {
  20. //stack1节点顺序弹出并压入stack2
  21. if (stack2.isEmpty()) {//条件是: stack2为空,而不是stack1非空, 这样摊还复杂度O(1)
  22. while (!stack1.isEmpty()) {
  23. stack2.push(stack1.pop());//stack1弹出节点并压入stack2
  24. }
  25. }
  26. return stack2.peek();
  27. }
  28. public boolean empty() {
  29. return stack1.isEmpty() && stack2.isEmpty();
  30. }
  31. }

Python:

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

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

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

  1. class MyQueue:
  2. def __init__(self):
  3. self.queue = []
  4. def push(self, x: int) -> None:
  5. self.queue.append(x)
  6. def pop(self) -> int:
  7. #弹出第一个元素
  8. return self.queue.pop(0)
  9. def peek(self) -> int:
  10. #返回第一个元素
  11. return self.queue[0]
  12. def empty(self) -> bool:
  13. 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. selenium三大切换的骚操作之显性等待

    一.handle窗口切换 当点击某个元素后,会重新生成一个新的页签,但此时我们的操作仍然在原先的窗口当中,如果要在新的窗口继续操作元素,那么就要用到handle窗口切换的方法. 常用方法: windo ...

  2. 『嗨威说』算法设计与分析 - 回溯法思想小结(USACO-cha1-sec1.5 Checker Challenge 八皇后升级版)

    本文索引目录: 一.回溯算法的基本思想以及个人理解 二.“子集和”问题的解空间结构和约束函数 三.一道经典回溯法题点拨升华回溯法思想 四.结对编程情况 一.回溯算法的基本思想以及个人理解: 1.1 基 ...

  3. 基于bert的命名实体识别,pytorch实现,支持中文/英文【源学计划】

    声明:为了帮助初学者快速入门和上手,开始源学计划,即通过源代码进行学习.该计划收取少量费用,提供有质量保证的源码,以及详细的使用说明. 第一个项目是基于bert的命名实体识别(name entity ...

  4. Python数据分析揭秘知乎大V的小秘密

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 清风小筑 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  5. Java生鲜电商平台-SpringCloud微服务架构中核心要点和实现原理

    Java生鲜电商平台-SpringCloud微服务架构中核心要点和实现原理 说明:Java生鲜电商平台中,我们将进一步理解微服务架构的核心要点和实现原理,为读者的实践提供微服务的设计模式,以期让微服务 ...

  6. [转]C++字符串操作函数_tcschr、_tcsrchr

    原文出处:https://blog.csdn.net/eickandy/article/details/50083169 C++标准库函数提供了字符和字符串的操作函数,并提供了其UNICODE版本,本 ...

  7. SQLi-LABS Page-3 (Stacked injections) Less-38-Less-45

    Less-38 堆叠注入原理简介堆叠注入简介 Stacked injections: 堆叠注入.从名词的含义就可以看到应该是一堆 sql 语句(多条)一起执行.而在真实的运用中也是这样的, 我们知道在 ...

  8. postman---postman文件夹介绍以及批量执行用例

    我们在做测试的过程中,都会多次请求接口,都会把接口保存下来,方便下次直接请求,节省时间不用每次都重新输入,我们一起看下Postman如何保存接口会话 保存请求作用 Postman可以将各个请求组合保存 ...

  9. spark-shell 中rdd常用方法

    centos 7.2     spark 2.3.3      scala 2.11.11    java 1.8.0_202-ea spark-shell中为scala语法格式 1.distinct ...

  10. 博客美化——Silence主题皮肤

    介绍   一款专注阅读的博客园主题,主要面向于经常混迹 博客园 的朋友.其追求大道至简的终极真理,界面追求简洁.运行追求高效.部署追求简单. 博客皮肤源码地址 预览地址 如何部署.使用皮肤 Silen ...