40. Implement Queue by Two Stacks

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

The queue should support push(element)pop() and top() where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

Example

Input
push(1)
push(2)
push(3)
push(4)
push(5)
pop()
pop()
push(6)
push(7)
push(8)
push(9)
pop()
pop()
Expected

[1,2,3,4]

思路:

用两个stack,实现一个queue。

push到stack2。pop的时候,先判断stack1是否为空:如果不为空(说明上一轮push进stack1的,没有pop完),直接stack1.pop(); 如果为空,则把stack2所有元素全部push到stack1,(保证第一个push到stack2的元素最后push进stack1),再stack1.pop().

注意:

  1. 创建两个类变量,在构造方法中初始化两个类变量stack1, stack2.
  2. 代码改进,line 17,18 和 line 26,27 重复,可以封装成一个方法。

代码:

 public class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
} public void push(int element) {
stack2.push(element);
} public int pop() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
return stack1.pop();
} public int top() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
return stack1.peek();
}
}

代码改进:

public class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
} public void stack2ToStack1(){
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
} public void push(int element) {
stack2.push(element);
} public int pop() {
if (stack1.isEmpty()) {
this.stack2ToStack1();
}
return stack1.pop();
} public int top() {
if (stack1.isEmpty()) {
this.stack2ToStack1();
}
return stack1.peek();
}
}

Lintcode40-Implement Queue by Two Stacks-Medium的更多相关文章

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

  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. Implement Queue by Two Stacks

    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

    1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...

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

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

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

  10. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. 四、XML语言学习(3)

    XML编程(CURD) 1.XML解析技术概述XML解析方式分为两种:DOM方式和SAX方式DOM:Document Object Model,文档对象模型.这种方式是W3C推荐的处理XML的一种方式 ...

  2. php 字符串截取,支持中文和其他编码

    function.php //使用方法 $content= mb_substr($content,0,25,'utf-8'); /** * 字符串截取,支持中文和其他编码 * @static * @a ...

  3. shell符号

    *:  通配符 *.c : c结尾的文件 *v : v结尾的文件 v* : v开头的文件

  4. JavaScript基础知识(数据类型)

    数据类型 布尔:true/fasle console.log(typeof true);// "boolean" Number : true -->1 false --> ...

  5. Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  6. openERP笔记,自定义开发模块

    ##目标 OpenERP模块基本结构 使用模块添加额外的字段(Date Required和Rush Order) 扩展视图, 让OpenERP能够显示新的字段 修改用于OpenERP工作流的可用状态 ...

  7. mysql脚本手动修改成oracle脚本

    今天有一个需求,立了一个新项目,新项目初步定了使用了现有的框架,但数据库要求由原来的mysql改成oracle,所以原来的基础版本的数据库脚本就需要修改成符合oracle的脚本,修改完成后,总结了一下 ...

  8. Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法

    在进行 Java AES 加密测试时,出现如下错误信息: java.security.InvalidKeyException: Illegal key size or default paramete ...

  9. webpack项目搭建

    1.新建一个文件目录,命令行进入当前目录,输入npm init 创建package.json文件 2.安装项目依赖webpack模块: npm install webpack --save-dev 3 ...

  10. cocos2d-x在Android上的编译过程(3):简化Android.mk文件的编写

    在编译动态库时.要求我们要去编写jni/Android.mk文件.告诉编译器编译出来的库时应包括包括编译文件和其它引用库.但对于一个大项目来说,维护这个文件肯定是一件比較繁琐的事情.由于每加一个文件或 ...