用Queue模拟Stack,用一个变量记录头就行了。

Time: pop O(n) 其余 O(1)
Space: O(n)

讨论区有人给出了O(1)的办法。。牛逼。 我就不考虑了。

class MyStack {Queue<Integer> q = new LinkedList<Integer>();int head = 0;// Push element x onto stack.public void push(int x) {q.add(x);head = x;}// Removes the element on top of the stack.public void pop() {Queue<Integer> temp = new LinkedList<>();while (q.size() != 1) {temp.add(q.poll());}q.poll();while (!temp.isEmpty()) {q.add(temp.peek());head = temp.poll();}}// Get the top element.public int top() {return head;}// Return whether the stack is empty.public boolean empty() {return q.isEmpty();}
}

转载于:https://www.cnblogs.com/reboot329/articles/6034680.html

225. Implement Stack using Queues相关推荐

  1. LeetCode 225. Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) – Push element x onto stac ...

  2. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

  3. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

  4. LeetCode 225. Implement Stack using Queues--用队列实现栈--C++解法

    LeetCode 225. Implement Stack using Queues–用队列实现栈–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在 ...

  5. C#LeetCode刷题之#225-用队列实现栈(Implement Stack using Queues)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4106 访问. 使用队列实现栈的下列操作: push(x) -- ...

  6. leetcode- 225 Implement Stack using Queues

    stack:  先进后出 pop(); push(); top(); size(); empty() queue: 先进先出 pop(); push(); front(); back(); size( ...

  7. LeetCode225. Implement Stack using Queues

    方法1:只用一个队列 出栈直接出队,但是入栈前先直接入队,然后出队n-1个元素并入队,这样刚进队的元素就在队头了. 上代码: class MyStack {private Queue<Integ ...

  8. LeetCode 所有题目总结

    文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...

  9. 5专题总结-数据结构

    专题5-数据结构 2017-07-13 C++ Primer P329好好研读,stack,queue,priority_queue都是顺序容器适配器adaptor.(接受一种已有的容器类型,使其行为 ...

最新文章

  1. 【Visual Studio 扩展工具】使用ComponentOne中的GroupDefinition和SortDefinition属性保存和还原布局...
  2. redis hash结构如何设置过期时间
  3. pc模式 华为mate30_华为mate30与电脑连不上怎么回事
  4. 【转】探讨android更新UI的几种方法----不错
  5. ISP PIPLINE (六) AWB
  6. 调查问卷Html5发展综述
  7. 第十二章 trie路由--基于Linux3.10
  8. 网页设计软件html图标,网页设计Photoshop(PS)CSS切图必用工具
  9. 【ExtJs】Extjs RowNumberer序号分页递增
  10. 最速下降法matlab全局最小值_最速下降和Newton法:Matlab实现
  11. Microsoft JET Database Engine 错误 '80004005' 操作必须使用一个可更新的查询。问题解决办法
  12. Win11如何调整鼠标dpi?Win11调整鼠标dpi的方法
  13. DART语言学习整理
  14. 生态 | 协同办公软件发展任重道远——基于数据库领域视角
  15. Unity 3D 入门小游戏 小球酷跑(上)
  16. 计算机瑞士留学经验,瑞士留学生活分享
  17. mysql5.7应该导什么包_限定盲盒!只有快递小哥可以拆的“战马空投箱”…到底什么来头?...
  18. 关于纯净的win7旗舰版安装后无无线网驱动问题
  19. 2022081班李亚楠20220920
  20. 加速C++程序编译的方法

热门文章

  1. SQL 常用数据类型汇总
  2. ViewPager 在 ScrollView 中显示不全的解决方法
  3. AR/QCA SPI 启动原理和 ART 地址定位原理
  4. UNIX再学习 -- 静态库与共享库
  5. zcmu1862(模拟)
  6. Darknet_Yolov4实战(二)_安装OpenCV
  7. Leetcode-移除链表元素
  8. Fabric 学习笔记-架构初探
  9. Android Framework------之Keyguard 简单分析
  10. python运算符讲解_3.Python运算符详解