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 ...
随机推荐
- Linux下Gcc生成和使用静态库和动态库详解(转)
一.基本概念 1.1什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行. 由于windows和linux的平台不同( ...
- C++中常见错误整理(不定期更新)
1.cannot have cv-qualifier 在C++中CV指const和volatile,非成员函数和静态成员函数不能有CV限定.
- java多线程系列之 synchronized
一.synchronized基本原理 java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁. ...
- webdriver无法打开firefox
1.执行webdriver.firefox()报错,提示: WebDriverException: Message: "Can't load the profile. Profile Dir ...
- 011-Scala中的apply实战详解
011-Scala中的apply实战详解 object中的apply方法 class中的apply方法 使用方法 apply方法可以应用在类或者Object对象中 class类 必须要创建实例化的类对 ...
- 《我是一只IT小小鸟》读后感
过了半个学期的大学生活,说实话,我是迷茫的,因为我还没有足够的了解IT这门课程,也不知道怎么学好这门课程. 直到老师推荐我们读一本书<我是一只it小小鸟>,起初,我并不认为它是一本多么好的 ...
- log4j日志-liu
log4j日志级别: http://michales003.iteye.com/blog/1160605 log4j日志配置详解: http://www.cnblogs.com/ITtangtang/ ...
- C# 学习电子书资料分享mobi epub等格式
.NET之美:.NET关键技术深入解析 作者: 张子阳 出版社:机械工业出版社 大小:20.29MB 出版时间:14-01-01 格式:mobi ASP.NET 4权威指南 作者: 马伟 出版社: 大 ...
- Read Excel file from C#
Common way is: var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirec ...
- X3850M2安装CertOS 7 KVM 2--DMMP
1,在DS8000中调整vg为单台服务器.检查另一台服务器内已经没有磁盘信息. 2,在余下的服务器中安装DMMP. 参考:http://edwin-wang.com/2012/08/device-ma ...