1 题目

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

    Notes:
  • You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

接口: 实现4个方法

2 思路

用2个stack,inboxoutbox

Queue:

  • Push the new element onto inbox

Dequeue:

  • If outbox is empty, refill it by popping each element from inbox and pushing it onto outbox
  • Pop and return the top element from outbox

each element will be in each stack exactly once - meaning each element will be pushed twice and popped twice, giving amortized constant time operations.(用这个方法,每个元素只在两个stack中存储一份,每个元素将会被push pop两次,MyQueue的pop操作的时间复杂度是分摊的时间常数,最好O(1),最坏O(n)).

复杂度:push O(1); pop O(1) or O(n); peek O(1) or O(n); empty O(1)

3 代码

MyQueue.java

import java.util.LinkedList;

// Java编程思想推荐在使用stack的时候,用LinkedList替代。
class MyQueue {
LinkedList<Integer> inbox = new LinkedList<Integer>();
LinkedList<Integer> outbox = new LinkedList<Integer>(); // Push element x to the back of queue.
public void push(int x) {
inbox.push(x);
} // Removes the element from in front of queue.
public void pop() {
if (outbox.isEmpty()) {
while (!inbox.isEmpty()) {
outbox.push(inbox.pop());
}
}
outbox.pop();
} // Get the front element.
public int peek() {
if (outbox.isEmpty()) {
while (!inbox.isEmpty()) {
outbox.push(inbox.pop());
}
}
return outbox.peek();
} // Return whether the queue is empty.
public boolean empty() {
return inbox.isEmpty() && outbox.isEmpty();
}
}

4 总结

  • 巧妙的用两个statck实现queue,数据只存储一份,很好的考察基本的数据结构能力。
  • 由于题目假设poppeek都不会在队列为空的时候执行,避免了Null Pointer Exception.

5 参考

lc面试准备:Implement Queue using Stacks的更多相关文章

  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. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  6. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  7. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  8. [LC] 232. Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  9. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

随机推荐

  1. 我的 ubuntu 12.04.2修复Grub

    网上有很多…… 首先我做了U盘启动,然后进入LiveCD模式. 输入grub,提示说要重新安装,好了,那就安装吧,连好网,sudo apt-get install grub 安装成功后,sudo -i ...

  2. JavaScript入门(2)

    一.JS输出内容--(document.write) document.write()可用于直接向HTML输出流写内容,即直接在网页中输出内容. 第一种:输出内容用" "括起来,直 ...

  3. java: Eclipse jsp tomcat 环境搭建(完整)

    ] 欢迎您! 要学习一门语言,首先要做的就是搭建环境,然后能写一个小的Demo(类似Helloworld),不仅可以建立信心,而且还可以为之后的学习搭建一个验证平台,事半功倍. net领域的vs,号称 ...

  4. C#DbHelperMySQL数据库帮助类 (转载)

    主要功能如下数据访问抽象基础类 主要是访问Mysql数据库主要实现如下功能 .得到最大值 .是否存在 .是否存在(基于MySqlParameter) .执行SQL语句,返回影响的记录数 .执行MySq ...

  5. NFC手机

    NFC手机 NFC手机内置NFC芯片,比原先仅作为标签使用的RFID更增加了数据双向传送的功能,这个进步使得其更加适合用于电子货币支付:特别是RFID所不能实现的,相互认证和动态加密以及一次性钥匙(O ...

  6. 一步一步建MVC

    http://www.cnblogs.com/yuangang/p/5569518.html

  7. Android增加监听的三种实现方式

    在Android中,为一个按钮增加监听的方式有五种 1.匿名内部类 @Override protected void onCreate(Bundle savedInstanceState) { sup ...

  8. Membership角色与权限管理

    安全性:成员资格与角色:验证与授权. 一.建数据库:在VS工具中用DOS环境执行ASPNET_REGSQL 二.配置程序访问数据库: > 在web.config之中加入 <connecti ...

  9. angular 基础练习

    <!DOCTYPE HTML> <html> <head> <title> 测试页 </title> <meta charset=&q ...

  10. 寒假ACM训练(二)

    放了假的效率明显就低起来,最近也一直在学习Ubuntu,所以一直等到今天才写. 还是在用PC. 真的十分郁闷这个LC-Display,其实从思路上是有很多.不过我最后把他当成8字,分成了七笔. 一直W ...