Implement Queue by Stacks

原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/#

As the title described, you should only use two stacks to implement a queue's actions.

The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

样例

For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2

挑战

implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.

SOLUTION 1:

使用两个栈,stack1和stack2。

http://www.ninechapter.com/problem/49/

对于Queue的操作对应如下:
Queue.Push:
push到Stack1
Queue.Pop:
如果Stack2非空,Stack2.pop
否则将Stack1中的所有数pop到Stack2中(相当于顺序颠倒了放入),然后Stack2.pop()
每个数进出Stack1和Stack2各1次,所以两个操作的均摊复杂度均为O(1)

 1 public class Solution {
 2     private Stack<Integer> stack1;
 3     private Stack<Integer> stack2;
 4
 5     public Solution() {
 6        // do initialization if necessary
 7        stack1 = new Stack<Integer>();
 8        stack2 = new Stack<Integer>();
 9     }
10
11     public void push(int element) {
12         // write your code here
13         stack1.push(element);
14     }
15
16     public int pop() {
17         // write your code here
18         if (stack2.isEmpty()) {
19             while (!stack1.isEmpty()) {
20                 stack2.push(stack1.pop());
21             }
22         }
23
24         return stack2.pop();
25     }
26
27     public int top() {
28         // write your code here
29         // write your code here
30         if (stack2.isEmpty()) {
31             while (!stack1.isEmpty()) {
32                 stack2.push(stack1.pop());
33             }
34         }
35
36         return stack2.peek();
37     }
38 }

View Code

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/stack/StackQueue.java

Lintcode: Implement Queue by Stacks 解题报告相关推荐

  1. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  2. 使用栈实现队列 Implement Queue using Stacks

    为什么80%的码农都做不了架构师?>>>    问题: Implement the following operations of a queue using stacks. pus ...

  3. LeetCode 232. Implement Queue using Stacks

    题目: Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  4. C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)

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

  5. leetcode python3 简单题232. Implement Queue using Stacks

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

  6. 25行代码AC——习题5-7 打印队列(Printer Queue,UVa 12100)——解题报告

    励志用尽量少的代码做高效的表达. 题目(提交)链接→UVa-12100 题目描述: 我们需要用打印机打印任务.每个任务都有1~9间的优先级,优先级越高,任务越急. 打印机的运作方式:从打印队列里取出一 ...

  7. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

  8. leetcode 232. 用栈实现队列(Implement Queue using Stacks)

    目录 题目描述: 示例: 说明: 解法: 题目描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列 ...

  9. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

最新文章

  1. Windows10下SSH远程拷贝文件
  2. 阿里云API网关相关操作
  3. HuggingFace-transformers系列的介绍以及在下游任务中的使用
  4. 科大星云诗社动态20210329
  5. java死锁怎么用jvm调试,线程死锁演示,线程锁演示,模拟JVM的线程次序调度
  6. 一步一步写算法(之通用算法的编写)
  7. 近年来,学习图像去雾不得不看的论文和源代码
  8. NFine极速WEB + ORM框架源码
  9. CMMI3过程改进项目计划
  10. 屏幕录制专家——录制视频没声音的解决办法
  11. php校园一卡通系统
  12. 中国研修网计算机培训心得,网络研修培训心得体会
  13. 天下第一铭:汤晓鸥教授自叙的故事
  14. uc手机浏览器 手机模拟_移动端页面调试工具——UC浏览器开发者版
  15. 王道——数据结构 第一章 思维拓展
  16. PTK(Pulmonarytoolkit)环境搭建与 ITK4.13+VS2015的配置
  17. Chrome游览器改变SameSite设置
  18. 使用DirectShow技术切换双声道音频声道的方法
  19. html个人简历制作
  20. 数字图像处理(Digital Image Processing)

热门文章

  1. php fpm 日志级别,Php 错误日志级别
  2. php 实现错字检查,PHP每日一练:编写写字符串检查函数
  3. tcp丢包率_网络编程 | TCP/IP基础知识
  4. python一个星期可以入门吗_Python一星期入门第6篇: 模块和包
  5. form:errors path 不显示出错信息_视觉激光雷达信息融合与联合标定
  6. 令牌桶算法和漏桶算法python_限流之漏桶算法与令牌桶算法
  7. 小米平板位置服务器,小米平板 2
  8. 鸿蒙个人用户怎么申请,鸿蒙OS来了,这些机型的用户可以申请
  9. 高德地图怎么搜索marker_百度、高德、腾讯地图坐标认证怎么弄,3分钟轻松自助认证...
  10. 2029.石子游戏 IX-LeetCode