1 题目

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.

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

接口: 实现4个方法

2 思路

用2个queue来实现,java推荐用LinkedList作为Queue.

Version A: The stack should be efficient when pushing an item.

Version B: The stack should be efficient when popping an item.

Version A: push O(1); pop O(n)

push:

  • enqueue in queue1

pop:

  • while size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2
  • dequeue and return the last item of queue1, then switch the names of queue1 and queue2

Version B: push O(n); pop O(1)

push:

  • enqueue in queue2
  • enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2

pop:

  • deqeue from queue1

3 代码

Vesion A

class MyStackVersionA {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>(); // Push element x onto stack.
public void push(int x) {
q1.add(x);
} // Removes the element on top of the stack.
public void pop() {
top();
q1.poll();
Queue<Integer> tmp = q1;
q1 = q2;
q2 = tmp;
} // Get the top element.
public int top() {
int size = q1.size();
if (size > 1) {
int count = size - 1;
for (int i = 0; i < count; i++) {
q2.add(q1.poll());
}
}
return q1.peek();
} // Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}

Vesion B

class MyStackVersionB {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>(); // Push element x onto stack.
public void push(int x) {
q2.add(x);
int size = q1.size();
for(int i = 0; i < size; i++) {
q2.add(q1.poll());
}
Queue<Integer> tmp = q1;
q1 = q2;
q2 = tmp;
} // Removes the element on top of the stack.
public void pop() {
q1.poll();
} // Get the top element.
public int top() {
return q1.peek();
} // Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}

4 总结

  • 用两个queue实现stack, pop 和 push的效率的选择。
  • 由于题目假设poppeek都不会在队列为空的时候执行,避免了Null Pointer Exception.
  • stack 和 queue的相互实现,很好的考察基本功。

5 参考

lc面试准备:Implement Stack using Queues的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【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( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

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

  5. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  6. [LC] 225. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. ubuntu16.04 : 4: [: y: unexpected operator

    Ubuntu16.04 执行行脚本出错 在使用sh 执行脚本 出错标志 : 4: [: y: unexpected operator 原因:sh是连接到dash的,又因为dash跟bash的不兼容所以 ...

  2. CXF错误:Unsupported major.minor version 51.0,java.lang.UnsupportedClassVersionErro

    CXF错误:Unsupported major.minor version 51.0 java.lang.UnsupportedClassVersionError >>>>&g ...

  3. Java递归流程

    递归二字顾名思义就是:递过去,归回来.所以我索性叫它做有借有还吧. 下面的例子由c而来: public class Main {                                publ ...

  4. CSS background-position随笔

    1.定义和用法 background-position 属性设置背景图像的起始位置. 这个属性设置背景原图像(由 background-image 定义)的位置,背景图像如果要重复,将从这一点开始. ...

  5. CI框架篇之模型篇--初识(1)

    模型 模型是专门用来和数据库打交道的PHP类.例如,假设你想用CodeIgniter来做一个Blog. 你可以写一个模型类,里面包含插入.更新.删除Blog数据的方法. 下面的例子将向你展示一个普通的 ...

  6. [综合|基础|语法] 最新的皮肤帮助类 UI_Misc_Helper (转载)

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ...

  7. Wi-Fi无线网络下行速度超级慢 (5kb/s)之解决方案

    转载:http://www.iplaysoft.com/wifi-slow-solution.html 作者:X-Force 转载原因:该文分类提出了多种解决方案,并详述其原因.简洁清晰,可作为参考方 ...

  8. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  9. VS 2012中消失了的Create UnitTest

    前言:最近正在研究一个新项目的开发工作,这个项目的要求是必须写UnitTest,对于我个人来讲是很不喜欢写UnitTest的感觉这个东西会很大程度的延误开发进度,所以之前项目的UnitTest是能不写 ...

  10. windows phone 之ListBox模板选择

    有时做项目时,会遇到一种情况:需要把获取到的数据集合显示到首页,比如新闻,但是: 新闻也分类别啊,比如:图片类新闻.文字类新闻.视频类新闻. 那我们可能采用的模板就不一样了,那么,如何根据类别来选择模 ...