题目

用栈实现队列

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

队列应支持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. arcobject 相关

    要素添加: http://resources.esri.com/help/9.3/arcgisengine/arcobjects/esriGeoDatabase/IFeatureClass.Inser ...

  2. javascript 创建对象及对象原型链属性介绍

    我们知道javascript里定义一个普通对象的方法,如: let obj = {}; obj.num = 1; obj.string = 'string'; obj.func = function( ...

  3. jquery数组之存放checkbox全选值示例代码

    使用jquery数组可以存放checkbox全选值,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: <input type="checkbox" id=&q ...

  4. c#读写注册表示例分享

    c#读写注册表示例,示例中有详细注释. 代码: //写注册表RegistryKey regWrite;//往HKEY_CURRENT_USER主键里的Software子键下写一个名为“Test”的子键 ...

  5. 使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)

    概述 Velocity是微软推出的分布式缓存解决方案,为开发可扩展性,可用的,高性能的应用程提供支持,可以缓存各种类型的数据,如CLR对象. XML.二进制数据等,并且支持集群模式的缓存服务器.Vel ...

  6. css透明(支持各浏览器)

    opacity: 0.4;filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40); -ms-filter: "progid:D ...

  7. .net视图中日期格式化

    昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...

  8. WPF解析TTF 字体

    偶遇需要自己解析 TTF 字体并显示,此做... using System; using System.Collections.Generic; using System.Drawing.Text; ...

  9. Eclipse配置默认的编码集为utf-8

    既然开了博,那就来点有用的. 可以使用下面的方法,让Eclipse对所有的项目里所有文件都按照指定的编码解析. Eclipse安装目录下有一个eclipse.ini文件, 用记事本打开即可,在最后一行 ...

  10. SqlServer 系统存储过程

    exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns Categories;--查看列exec sp_helpIndex Cat ...