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

class MyStack {private Queue<Integer> queue=new LinkedList<>();/** Initialize your data structure here. */public MyStack() {}/** Push element x onto stack. */public void push(int x) {queue.add(x);for(int i=1;i<queue.size();i++){queue.add(queue.poll());}}/** Removes the element on top of the stack and returns that element. */public int pop() {return queue.poll();}/** Get the top element. */public int top() {return queue.peek();}/** Returns whether the stack is empty. */public boolean empty() {return queue.isEmpty();}}

方法二:采用两个队helpQ和pollQ
入栈时先将pollQ的元素出队并存入helpQ中,然后新元素进pollQ,再将helpQ中的所有元素出队依次进pollQ
代码:

class MyStack {private Queue<Integer> pollQ=new LinkedList<>(); private Queue<Integer> helpQ=new LinkedList<>();/** Initialize your data structure here. */public MyStack() {}/** Push element x onto stack. */public void push(int x) {while(!pollQ.isEmpty()){helpQ.add(pollQ.poll());}pollQ.add(x);while(!helpQ.isEmpty()){pollQ.add(helpQ.poll());}}/** Removes the element on top of the stack and returns that element. */public int pop() {return pollQ.poll();}/** Get the top element. */public int top() {return pollQ.peek();}/** Returns whether the stack is empty. */public boolean empty() {return pollQ.isEmpty();}}

如有错误,请指正!

LeetCode225. Implement Stack using Queues相关推荐

  1. leetcode- 225 Implement Stack using Queues

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

  2. LeetCode 225. Implement Stack using Queues

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

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

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

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

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

  5. 225. Implement Stack using Queues

    用Queue模拟Stack,用一个变量记录头就行了. Time: pop O(n) 其余 O(1) Space: O(n) 讨论区有人给出了O(1)的办法..牛逼. 我就不考虑了. class MyS ...

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

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

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

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

  8. python实现栈的操作_python简单实现队列和栈push、pop操作

    栈: # -*- coding: utf-8 -*- #定义序列 lst=[] def pop(): if(len(lst)==0): print"栈为空","无法出栈& ...

  9. mysql 存储过程 out 用处_mysql存储过程 OUT or INOUT argument 3 for routine

    mysql存储过程出现: OUT or INOUT argument 3 for routine gotask.UserLogin is not a variable or NEW pseudo-va ...

最新文章

  1. 学习观察神经网络:可视化整个模型训练中卷积层的激活
  2. BZOJ1935 园丁的烦恼
  3. ASP.NET开发常用代码
  4. 内核对象用于线程同步
  5. NAND FLASH分区规划
  6. Mysql中用between...and...查询日期时注意事项
  7. 如何实现A星寻路算法 Cocos2d-x 3.0 beta2
  8. Eclipse的.properties文件输出中文成unicode编码
  9. ESP32 开发笔记(四)LVGL控件学习 Roller 滑动选择控件
  10. 链路聚合LACP配置与结果验证
  11. 微信小程序点击事件(bindtap)传递参数的方法
  12. Python获取基金收益计算
  13. 炼数成金--支持向量机 笔记
  14. 什么是make/Makefile?
  15. java uniapp旅游微信小程序的开发hbuilderx
  16. 金融业信贷风控算法2-初等统计理论
  17. 为什么祖传代码被称为「屎山」?这个回答简直太形象了
  18. 记一次Very Animation动画插件使用
  19. java+小时转为分钟_用java将12862秒转换成 N 小时,K 分钟,M 秒
  20. three.js 实现辉光(原生JS)

热门文章

  1. [解决方案] Mendelay无法打开pdf文档:显示 unable to open this file
  2. 求圆形矩形面积java_Java求矩形面积和圆形面积的异常处理实例
  3. 小小的面试二分题目,竟然暗藏玄机?
  4. python 儿童 游戏_python程序:两个小孩玩剪刀石头布游戏,一人十分,赢一次得一分,输一次减一分,平手不扣分,当没...
  5. OC语言 实现十进制字符串转二进制
  6. vue项目设置服务器地址,vue项目配置后端服务器地址
  7. 计算机思维在化工方面的应用,计算思维在化学上的应用.pdf
  8. 关于Paxos的历史
  9. 今日报错系列:没有找到 或上一个增量链接没有生成它;正在执行完全链接
  10. 架构师成长之路工具篇(1):markdown撰写文档