lintcode :implement queue by two stacks 用栈实现队列
题目
用栈实现队列
正如标题所述,你需要使用两个栈来实现队列的一些操作。
队列应支持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 用栈实现队列的更多相关文章
- LintCode Implement Queue by Two Stacks
1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...
- 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) -- ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- 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 ...
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- HTML5 INPUT新增属性
HTML5的input标签新增了很多属性,也是让大家非常兴奋的一件事,用简单的一个属性搞定以前复杂的JS验证.input新增的这些属性,使得html和js的分工更明确了,使用起来十分舒畅.我们先看下i ...
- SharedPreferences的基本用法
获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...
- 如何使用NET Reactor为您的.Net(C#,VB.Net) 源代码加密
前言 VS开发的源代码安全性,是很多开发者头痛的事情.于是保护好源代码便成了开发者们最关心的事情之一了. 在网上搜一搜,很多有不少的第三方工具可以为源代码加密.加密方式不外乎就是混淆,加壳. 理论上, ...
- php中fopen函数用法详解(打开文件)
介绍下php中的fopen函数. 1.resource fopen(string $filename, string $mode [,bool $use_include_path [, resou ...
- php实现查询百度google收录情况(示例代码)
对了貌似查google pr的东西只是file一个地址而已,如此说了就没有什么难度了.完整代码如下 写了一个小东西记录baidu和google对于站点的收录情况,现在可以查询了,其实也没什么难度,就是 ...
- 2013-07-23 IT 要闻速记快想
### ========================= ###苹果的新动向今天华尔街日报称,苹果正在测试13英寸的大号iPad,以及更大屏幕的iPhone.而早在五月份,韩国资讯网站 ET New ...
- plateform_driver_register和plateform_device_register区别
设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. 以一个USB设备为例,有两种情形: (1)先插上USB设备并挂到总线中,然后在安装USB驱动程序过程中从总线上遍历各个设备,看驱 ...
- 微软职位内部推荐-Principal Software Developer
微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com ) Work Location: Suzhou/Beijing News is ...
- android camera2 Api(转载)
现在的手机一般都会提供相机功能,有些相机的镜头甚至支持1000万以上像素,有些甚至支持光学变焦,这些手机已经变成了专业数码相机.为了充分利用手机上的相机功能,Android应用可以控制拍照和录制视频. ...
- 移植linux4.7.2与ubifs到jz2440
前言 整个暑假跟着韦东山的视频和书籍移植了linux2.3.6到jz2440,现在自己尝试移植linux4.7.2到板子上,并使用ubifs文件系统代替旧的jffs2文件系统. 下载交叉编译工具链 工 ...