题目

Implement Stack using Queues

Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.

pop() – Removes the element on top of the stack.

top() – Get the top element.

empty() – Return whether the stack is empty.

Example:

MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false
```Notes:You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).# Stack解法
双链表解法,push时间复杂度为O(1), pop时间复杂度为O(n)。
```javaclass MyStack {private Queue<Integer> q1 = new LinkedList<>();private Queue<Integer> q2 = new LinkedList<>();private int top;/** Initialize your data structure here. */public MyStack() {}/** Push element x onto stack. */public void push(int x) {q1.add(x);top = x;}/** Removes the element on top of the stack and returns that element. */public int pop() {while (q1.size() > 1) {top = q1.remove();q2.add(top);}int i = q1.remove();Queue<Integer> temp = q1;q1 = q2;q2 = temp;return i;}/** Get the top element. */public int top() {return top;}/** Returns whether the stack is empty. */public boolean empty() {return q1.isEmpty();}
}/*** Your MyStack object will be instantiated and called as such:* MyStack obj = new MyStack();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.top();* boolean param_4 = obj.empty();*/

算法: 用队列Queue实现栈Stack相关推荐

  1. 队列Queue 先进先出 栈Stack 先进后出

    1 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System ...

  2. 看动画学算法之:队列queue

    文章目录 简介 队列的实现 队列的数组实现 队列的动态数组实现 队列的链表实现 队列的时间复杂度 简介 队列Queue是一个非常常见的数据结构,所谓队列就是先进先出的序列结构. 想象一下我们日常的排队 ...

  3. 有苦有乐的算法 --- 使用队列结构实现栈结构

    题目 队列实现栈 解析 首先准备两个队列queue1.queue2 数据[1,2,3,4,5]依次入队queue1,之后把[1,2,3,4]依次出兑在入队到queue2,[5]留在queue1中 此时 ...

  4. 栈和队列:1.栈(Stack)

    栈,线性表的一种特殊的存储结构.与学习过的线性表的不同之处在于栈只能从表的固定一端对数据进行插入和删除操作,另一端是封死的. 图1 栈结构示意图 由于栈只有一边开口存取数据,称开口的那一端为" ...

  5. bash shell数组模拟队列queue和shell数组使用技巧

    一 shell数组操作模拟队列queue或者栈stack http://www.tech-recipes.com/rx/911/queue-and-stack-using-array/ here is ...

  6. java数据结构与算法之(Queue)队列设计与实现

    [版权申明]转载请注明出处(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/53375004 出自[zejian的博客] ...

  7. abcde依次进入一个队列_数据结构与算法 | 一文掌握队列Queue(真题讲解)

    本系列内容专为课程面向笔/面试的<数据结构与算法>总结性精讲开设,以图文并茂的方式讲解数据结构,让大家打牢基础,促进对课程内容的掌握,最后做到题解大神,大厂offer拿到手软! 目录:一文 ...

  8. Java栈 Stack

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120830358 本文出自[赵彦军的博客] Java队列 Queue Java队列 ...

  9. Java队列 Queue

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120828046 本文出自[赵彦军的博客] Java队列 Queue Java队列 ...

  10. python数据结构和算法 时间复杂度分析 乱序单词检测 线性数据结构 栈stack 字符匹配 表达式求值 queue队列 链表 递归 动态规划 排序和搜索 树 图

    python数据结构和算法 参考 本文github 计算机科学是解决问题的研究.计算机科学使用抽象作为表示过程和数据的工具.抽象的数据类型允许程序员通过隐藏数据的细节来管理问题领域的复杂性.Pytho ...

最新文章

  1. AI 版 Nature Index 排名,两种结果折射中国 AI 实力软肋
  2. 史上最强音视频下载神器youtube-dl回归,GitHub75k星
  3. jquery设置复选框为只读_checkbox设置复选框的只读效果不让用户勾选
  4. 【vue】介绍一个vuejs 和 element 搭建的一个后台管理界面
  5. netca error
  6. SpringBoot 快速开启事务(附常见坑点)
  7. 【推荐】揭秘谷歌电影票房预测模型
  8. 闭锁java_java多线程学习十::::CountDownLatch闭锁
  9. HoloLens开发手记- SpectatorView for iOS编译指南
  10. java读c二进制文件_如何使用JAVA读取C / Matlab创建的二进制文件
  11. 取消Win7关机时的补丁更新
  12. 多克隆,坚决抵制快照
  13. ASP.NET 分页技术
  14. 卡巴斯基7.0离线更新升级包病毒库
  15. sql 根据省份证号码提取年龄
  16. 未来机器人背景的AI人工智能PPT模板
  17. java调用海康人脸识别机5603的sdk的使用总结(三)
  18. VMware虚拟机安装黑群晖7.1
  19. java程序设计概念对象先行_Java程序设计概念:对象先行(原书第8版)简介,目录书摘...
  20. Android View绘制流程

热门文章

  1. linux的mysql不允许连接_linux下允许mysql远程连接
  2. 妙启动_十张图带你了解中国国产奶酪巨头——妙可蓝多发展情况
  3. laravel 模型事件 updated 触发条件
  4. 考拉升级https经验
  5. java练习题-求int类型N值的阶乘
  6. 2017 ZSTU寒假排位赛 #5
  7. 英特尔® 实感™ SDK 架构
  8. 试试Navicat和Axere RP Pro吧
  9. 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
  10. WP7 手机软件纪念 - 稍后读软件