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. 《Android深度探索》(卷1)HAL与驱动开发读后感:

    第一章:安卓系统移植与驱动开发概述 全书分为4篇,分别从搭建开发环境,Linux驱动和Android HAL的基础知识,开发Linux驱动的高级技术和分析典型的Linux驱动源代码4个方面介绍Andr ...

  2. pickle 序列化反序列化

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  3. vs13的内存占用 关闭之

    .如何关闭CodeLens呢? 在VS菜单栏 >> 工具 >> 选项 >> 文本编辑器 >> 所有语言 >> CodeLens In VS ...

  4. linux命令每日一练习 显示一个文件的制定行---查看命令帮助信息

    sed -n '11,12p' ××× cat ***|tail -n +12|head -n 2 tail -n +5 显示末尾五行 查看一个命令的帮助 详细的 man *** 简略的 *** -- ...

  5. 高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载

    CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会 ...

  6. strace命令使用

    在调试的时候,strace能帮助你追踪到一个程序所执行的系统调用.当你想知道程序和操作系统如何交互的时候,这是极其方便的,比如你想知道执行了哪些系统调用,并且以何种顺序执行. 这个简单而又强大的工具几 ...

  7. Java第六次作业修改版

    import java.util.ArrayList; import java.util.Collections; import java.util.Random; public class Draw ...

  8. MongoDB学习

    最近在学习,参考一线码农的教程 http://www.cnblogs.com/huangxincheng/category/355399.html

  9. 修改HTTPD.CONF中的DocumentRoot,出现 You don't have permission to access /??? on this server.

    apache 2.4 修改/conf/extra/httpd-vhosts.conf <VirtualHost _default_:80> DocumentRoot '${SRVROOT} ...

  10. C# treeview 绑定数据 【转】

    private void bindTreeView1() { string sql = "select * from dm_category"; DataTable dt = db ...