题目

用栈实现队列

正如标题所述,你需要使用两个栈来实现队列的一些操作。

队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素。

pop和top方法都应该返回第一个元素的值。

样例

比如push(1), pop(), push(2), push(3), top(), pop(),你应该返回1,2和2

挑战

仅使用两个栈来实现它,不使用任何其他数据结构,push,pop 和 top的复杂度都应该是均摊O(1)的

解题

两个栈stack1 、stack2,一个存储入队列元素,一个存储出出队列元素

stack2 入队列

stack1出队列

当入队列的时候:直接在入栈stack2

当出队列的时候:若栈stack2中有元素,则栈stack2底的元素就是所要出队列的元素,将栈stack2中的元素全部出来,并放到栈stack1中,此处stack1栈顶元素就是答案

这里有个问题是若栈stack1中有元素,则就直接取出栈stack1的栈顶元素就是答案

Java

public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public Queue() {
// do initialization if necessary
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
private void stack2Tostack1(){
while(!stack2.empty()){
stack1.push(stack2.peek());
stack2.pop();
}
}
public void push(int element) {
// write your code here
stack2.push(element); } public int pop() {
// write your code here
if( stack1.empty()){
this.stack2Tostack1();
}
return stack1.pop();
} public int top() {
// write your code here
if(stack1.empty()){
this.stack2Tostack1();
}
return stack1.peek();
}
}

Java Code

Python

class MyQueue:

    def __init__(self):
self.stack1 = []
self.stack2 = [] def push(self, element):
# write your code here
self.stack2.append(element) def top(self):
# write your code here
# return the top element
self.stack2Tostack1()
return self.stack1[len(self.stack1)-1] def pop(self):
# write your code here
# pop and return the top element
self.stack2Tostack1()
return self.stack1.pop() def stack2Tostack1(self):
if len(self.stack1)==0:
while len(self.stack2)!=0:
self.stack1.append(self.stack2.pop())

Python Code

参考了九章 ,由于初始化不知道怎么定义

lintcode :implement queue by two stacks 用栈实现队列的更多相关文章

  1. LintCode Implement Queue by Two Stacks

    1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...

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

  3. Lintcode: Implement Queue by Stacks 解题报告

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

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

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

  5. 40. Implement Queue by Two Stacks【medium】

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

  6. LeetCode OJ:Implement Queue using Stacks(栈实现队列)

    比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...

  7. Implement Queue by Two Stacks

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

  8. LeetCode 232:Implement Queue using Stacks

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

  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. jQuery对input中radio的一些操作

    通过jQuery获取页面中的所有radio对象,遍历页面中的radio,取消选中的标签,因为使用到jQuery时间,因此引用到了网上公共的js,这只是本人的一些总结,大神勿喷. <html> ...

  2. Freezable 对象(WPF)

    # Freezable 对象(WPF) # > Freezable 继承自 DependencyObject,同时添加了 Freezable 方法,用于冻结对象. --- ## 冻结对象 ## ...

  3. C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法

    初学c#,因最近公司客户要求原出货标签需实现条码化,练手的机会来了,遂动手做这个程序,开始都是一些增删改查操作一直很顺利,但到RDLC报表将条码显示到报表上犯难了,因为初学未接触过报表,上网查资料均一 ...

  4. vim配置python开发环境

    vim配置python开发环境 一.安装vim sudo apt-get install vim 二.vim基础配置 #Centos6.5 /usr/share/vim/vim72 vi /etc/v ...

  5. LLVM language 参考手册(译)(2)

    调用约定(Calling Conventions) LLVM functions, calls and invokes 可以带有一个可选的调用约定来指明调用方式.每一对 caller/callee(调 ...

  6. ASP.NET Core 1.0

    .NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...

  7. 通过js获取DropDownList的选中项

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. Rac & DG

    Rac环境: RAC版本异同:[10R2,11R1(和10类似)],11R2,12c: 目录: 10.2的ASM需要单独的目录(oracle home):rdbms home,asm home, cr ...

  9. Android手机做无线中继路由器

    为什么要拿手机做路由器?因为我现在每天要带着一个火柴盒大小的路由器(703n).它提供了一个f了q的无线网络,电脑,手机,平板等设备连接上这个无线网络之后之后就可以自由上twitter,看youtub ...

  10. 无法解析的外部符号 _WinMain@16 fatal error LNK1120: 1 个无法解析的外部命令

    一,问题描述MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 ...