题目

用栈实现队列

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

队列应支持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. 【Qt】Qt之密码框不可选中、复制、粘贴、无右键菜单等【转】

    简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...

  2. jQuery select的操作代码

    jQuery對select的操作的实际应用代码. //改變時的事件  复制代码代码如下: $("#testSelect").change(function(){ //事件發生  j ...

  3. 一款非常炫酷的jQuery动态随机背景滚动特效

    一款非常炫酷的jQuery动态随机背景滚动特效 图片背景会不停息的滚动,带有那种漂浮的视觉效果,小圈圈飘动. 更好的是还兼容IE6浏览器,大伙可以好好研究研究. 适用浏览器:IE6.IE7.IE8.3 ...

  4. 定位表的数据块并且dump出来

    SQL> select * from city;         ID NAME ---------- ----------          7 Chicago          6 Jers ...

  5. NGUI3.5系列教程之 UILabel

    此NGUI版本为:3.5.1 NGUI 的UILabel脚本下的文字框可以用BBCode设置:[b]Bold[/b] 粗体 [i]italic[/i] 斜体 [u]underline[/u]下划线 [ ...

  6. ASP.NET Web – AJAX 回送

    使用UpdatePanel时要一起使用的控件是ScriptManager.ScriptManager类加载了包含几个功能的JavaScript函数.也可以使用这个类加载自己定制脚本.ScriptMan ...

  7. java NIO与IO的区别

    nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享. 首 ...

  8. 如何自定义Liferay 7 portal的Log in登录界面

    前提: 1. Liferay portal 7 2. Liferay IDE 3.0.1 Liferay现有的工具中提供了很多修改portal的模板,以满足开发者的各种自定义需求. 修改的原理是利用M ...

  9. 工程移除CocoaPods依赖库

    http://zanderzhang.gitcafe.io/2015/09/26/工程移除CocoaPods依赖库/ 点这里--->CocoaPods安装和使用教程 当我们工程安装很多第三方开源 ...

  10. C# book

    <编写高质量代码:改善C#程序的157个建议>源码下载 http://www.cnblogs.com/luminji/archive/2011/09/20/2182265.html < ...