LintCode Implement Queue by Two Stacks
1. stack(先进后出):
pop 拿出并返回最后值; peek 返回最后值; push 加入新值在后面并返回此值。
2. queue(先进先出) :
poll = remove 拿出并返第一个值; element = peek 返第一个值; add = offer 加入新值在后面并返回true/false。
做此题时, 第一个stack为基础, 第二个stack为媒介来颠倒顺序, 加时从第一个加, 取时从第二个取。
public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public Queue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
// do initialization if necessary
}
public void push(int element) {
stack1.push(element);
// write your code here
}
public int pop() {
if(!stack2.empty()){
return stack2.pop();
} else{
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
if(!stack2.empty()){
return stack2.pop();
} else return -1;
}
// write your code here
}
public int top() {
if(!stack2.empty()){
return stack2.peek();
} else{
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
if(!stack2.empty()){
return stack2.peek();
} else return -1;
}
// write your code here
}
}
LintCode Implement Queue by Two Stacks的更多相关文章
- 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 ...
- lintcode :implement queue by two stacks 用栈实现队列
题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...
- 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:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
随机推荐
- 线性控制原理——PID算法应用
使用控制系统(PID)控制被控对象 PID控制的三要素:控制器,被控对象,反馈器.控制器就是一个数学模型,就PID来说,等同于PID算法.是对反馈量的一个处理与输出.通俗的说就是对于每个被控的量,我的 ...
- java web学习之初识jsp
用java语言(+html语言)开发动态资源的技术: jsp的运行过程,1:tomcat服务器将jsp代码翻译成java代码,并且编译成class文件 2:tomcat服务器构造类对象 3:tomca ...
- Django的单元测试
1.单元测试框架 django-unittest https://docs.djangoproject.com/en/1.9/topics/testing/overview/#speeding-up- ...
- .net 读取Excel文件报错
错误内容 Microsoft Office Excel 不能访问文件“D:\WWWRoot\Website\Test\Excels\Test1.xls”. 可能的原因有: 1 文件名称或路径不存在. ...
- css 之 文本缩进属性(text-indent)
文章转自:http://www.10wy.net/Article/CSS/CSS_list_8.html查看更多更专业性的文章请到:网页设计网 文本缩进属性(text-indent) 这个属性设定文本 ...
- .Net Log4Net配置多文件日志记录
其他配置详情在网上都可以找到,但是很多看着都晕,本人就记录一下如何使用: 1.按不同级别(官方说明)可记录的日志级别有: Info.Warn.Error.Debug 2.可以按着四个配置四个输出日志路 ...
- Python学习笔记-抽象
懒惰即美德.代码量少. hasattr判断函数是否可用. 创建函数. def hello(name) return 'hello,'+name+'!' 文档化函数: 加注释(#开头) 文档字符串.函数 ...
- 简单高效快捷的百度地图demo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- Win7下Hyenae的安装
(1)下载 链接:http://sourceforge.net/projects/hyenae/ 资源:hyenae-0.36-1_fe_0.1-1-win32.exe (2)README --- ...
- It will affect staff as well.
Premier Foods has reduced its number of suppliers dramatically in the last 12 months. In 2013 it mad ...