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. MYSQL低权限读文件

    技巧:Reading files in MySQL with file_priv = no. 当用户无权限调用load_file()函数的时候可以用以下方式读取文件内容: 1.建立test(不分配fi ...

  2. (转载)jQuery 1.6 源码学习(二)——core.js[2]之extend&ready方法

    上次分析了extend方法的实现,而紧接着extend方法后面调用了jQuery.extend()方法(core.js 359行),今天来看看究竟core.js里为jQuery对象扩展了哪些静态方法. ...

  3. onethinkp导入excel

    /** * Excel导入函数 * @author crx349 */ if (!empty($_FILES)) { $config = array( 'maxSize' => 3145728, ...

  4. idea 中利用maven创建java web 项目

    转自:http://www.linuxidc.com/Linux/2014-04/99687.htm 本文主要使用图解介绍了使用IntelliJ IDEA 12创建Maven管理的Java Web项目 ...

  5. pandas 数据检索

    使用pandas的一些方法: 条件检索 按index取值

  6. 【转】Tomcat的默认访问路径

    放在外网的应用,用户多是直接输入域名访问,相信没有哪个后面还加个尾巴,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml ...

  7. Error Domain=ASIHTTPRequestErrorDomain Code=8 "Failed to move file from"xxx/xxx"to"xxx/xxx"

    今天真的好高兴呀 我解决了一个折磨了我一周的问题,真的是激动地要哭出来了,为了这个问题,我嘴也烂了,头发抓了一地啊.虽然解决方法,最后还是展现出了“百度”的伟大,但是我还是很开心,在这里我展示一下我的 ...

  8. Delphi编译的程序如何获取管理员权限

    1.制作manifest文件 <?xml version="1.0" encoding="UTF-8" standalone="yes" ...

  9. iOS推送生成服务器端p12文件

    生成服务器端推送p12文件 所需文件:A.开发证书  aps_production.cer B.本地导出的私钥   : aps_production.p12 C.生成证书时用到的请求文件:Push.c ...

  10. JQ源码学习-1-无new构建

    此文章仅为个人学习 Aaron的jQuery源码分析 笔记之用. 一:采用 构造函数 返回 原型初始化方法,原型初始化方法又返回构造函数 的方式代替new 但当 返回的却是‘web’而不是Object ...