Question

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 toppeek/pop from topsize, 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).

Solution

enQueue(q,  x)
1) Push x to stack1 (assuming size of stacks is unlimited). deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from satck1 to stack2.
3) Pop the element from stack2 and return it.

Check each element i, i can only be pushed/ poped once at stack 1, and pushed/ poped once at stack 2. So add(), remove(), peek() still cost O(1) time.

 public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public Queue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
} public void push(int element) {
stack1.push(element);
} public int pop() {
if (stack1.isEmpty() && stack2.isEmpty())
return 0;
if (stack2.isEmpty()) {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
return stack2.pop();
} public int top() {
if (stack1.isEmpty() && stack2.isEmpty())
return 0;
if (stack2.isEmpty()) {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
return stack2.peek();
}
}

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. [LeetCode] 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. 【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集

    近来复习编译原理,语法分析中的自上而下LL(1)分析法,需要构造求出一个文法的FIRST和FOLLOW集,然后构造分析表,利用分析表+一个栈来做自上而下的语法分析(递归下降/预测分析),可是这个FIR ...

  2. Java宝典(一)

    -switch语句能作用在byte上,能否作用在long上,能否作用在String上? -在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量,整数表达式可以是int基本类型或I ...

  3. 《Java程序员面试笔试宝典》之volatile有什么作用

    在由Java语言编写的程序中,有时候为了提高程序的运行效率,编译器会自动对其进行优化,把经常被访问的变量缓存起来,程序在读取这个变量的时候有可能会直接从缓存(例如寄存器)中来读取这个值,而不会去内存中 ...

  4. C#模拟网站用户登录

    我们在写灌水机器人.抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录.那么怎么用C#来模拟一个用户的登录拉?要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的. ...

  5. View中取设置了Tag的UILabel

    UILabel *badge = (UILabel *)[self.view viewWithTag:];

  6. 谷歌推出情境感知API

    在 Google I/O 2016 大会上,我们宣布推出新的 Google Awareness API,让您的应用可以利用快照和围栏智能应对用户情境,并且仅需占用极少量的系统资源. 所有开发者均可以通 ...

  7. 测缘分程序c代码简单实现

    #include<stdio.h>#include<stdlib.h>#include<math.h> #include <windows.h>//后面 ...

  8. ASP.NET导入Excel到SQL数据库

    protected void btnChange_Click(object sender, EventArgs e) { UserInfoClass tClass = (UserInfoClass)S ...

  9. CSS选择符 新的认识

    以前写选择符,很少使用">"符号,一般都是使用" "空格来代替.我觉得他们都是一样的功能,另一方面是为了方便.毕竟不用按住shift. 例如: .demo ...

  10. 我对 javascript 闭包的理解

    学js的学到闭包,但是理解不深. 后来看了一下这篇文章: 地址:http://leepiao.blog.163.com/blog/static/4850313020112835355917/ 内容如下 ...