题目

用栈实现队列

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

队列应支持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. file与 byte[] 互转

    byte 转file String filepath="D:\\"+getName();          File file=new File(filepath);        ...

  2. Selenium-RC Python 2.7 环境配置

    1.下载并安装Python http://www.python.org/getit/,我使用的是2.7.3的python版本 2.下载并安装setuptools[这个工具是python的基础包工具] ...

  3. js IDE WebStorm 注册码

    webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA  ...

  4. virtualbox centos安装增强工具

    系统的,VBoxLinuxAdditions-amd64.run 是用于64位系统的.执行以下命令来安装sh ./VBoxLinuxAdditions-x86.run 5.安装成功后重启系统.

  5. Java中main函数参数String args[] 和 String[] args 区别

    其实没什么区别的:当初我也是这样的疑问,呵呵:非要说区别就看下面:执行效果上没有不同, 但在语法意义上略有不同. 比如, String与String[], 前者叫字符串类型而后者叫字符串数组类型. S ...

  6. Swiper简单入门

    背景需求 给业务部分在m站实现一个邀请函的h5页面,基本流程:1.会议主题,2邀请函内容,3会议安排,4会议网络资源二维码,5酒店安排 技术分析 将ppt搬到h5上,每一页要用帧显示(这个没有用过). ...

  7. [转]一个基于完成端口的TCP Server Framework,浅析IOCP

    [转]一个基于完成端口的TCP Server Framework,浅析IOCP http://www.cppblog.com/adapterofcoms/archive/2010/06/26/1187 ...

  8. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据

    jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...

  9. json的序列化和反序列化支持时间格式转换

    .NET自带的json序列有时间格式问题,为了解决自己写了个json格式的序列化和反序列化 1.引入的命名空间 using System; using System.Collections.Gener ...

  10. Hello BaiduMap

    百度提供了地图的API,可以在android手机上用,这里其实只要参考百度给的文档就好了.因为api不断在更新,所以网上的博客感觉还是不太好,直接看官网的文档比较靠谱 这里照着百度文档上面提供的文档我 ...