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的更多相关文章

  1. 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) -- ...

  2. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  3. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  4. 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 ...

  5. lintcode :implement queue by two stacks 用栈实现队列

    题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...

  6. Implement Queue by Two Stacks

    As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...

  7. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  8. 【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( ...

  9. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

随机推荐

  1. c++学习笔记2

    T: 1.默认实参只能声明一次:  设计含有默认实参的函数时,要合理设置形参的顺序 :局部变量不能作为默认实参 2.内联机制用于优化规模较小.流程直接.频繁调用的函数 3.constexpr函数  返 ...

  2. android学习之ListView

    移通152余继彪 该组件用于显示列表的view  包含了三个关键元素 listView 适配器 以及数据,适配器主要是用于将数据映射到listview,适配器数据主要是有hasmap 配合list对每 ...

  3. java day2一个模拟双色球的代码

    package day2; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt ...

  4. BAE hibernate c3p0数据库连接池

    根据BAE官方文档:bae是不支持连接池的,但今天试验却能实现hibernate c3p0连接池,避免mysql连接超时 hibernate主配置文件hibernate.cfg.xml代码 <! ...

  5. CentOS 安装VNC Server

    环境 服务器:192.168.10.181 系统:CentOS 6.0 安装过程 1.切换至root用户 2.检测系统是否安装VNC [root@Nginx canyouNgx]# rpm -q vn ...

  6. Delphi 有关Dbgrideh控件:变色处理

    procedure OnDrawColumnCell( Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh; ...

  7. http://tool.oschina.net 在线API文档库java jquery ,php,很全的文档库

    http://tool.oschina.net  1.6API文档(中文)的下载地址: ZIP格式:http://download.java.net/jdk/jdk-api-localizations ...

  8. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  9. Python开发入门与实战17-新浪云部署

    17. 新浪云部署 上一章节我们介绍了如何在本地windows服务器部署python django的网站,本章我们简要说明一下如何把python django工程部署到云服务上. 本章章节我们描述如何 ...

  10. ie6下js更新元素display:block后,仍然不显示的hack办法

    $hotGames.html(html).removeClass("hide").show();//代码执行到这里,在ie6下仍然无法正常显示 //只有执行了下边的两行代码后,才正 ...