Lintcode40-Implement Queue by Two Stacks-Medium
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
push(1)
push(2)
push(3)
push(4)
push(5)
pop()
pop()
push(6)
push(7)
push(8)
push(9)
pop()
pop()
[1,2,3,4]
思路:
用两个stack,实现一个queue。
push到stack2。pop的时候,先判断stack1是否为空:如果不为空(说明上一轮push进stack1的,没有pop完),直接stack1.pop(); 如果为空,则把stack2所有元素全部push到stack1,(保证第一个push到stack2的元素最后push进stack1),再stack1.pop().
注意:
- 创建两个类变量,在构造方法中初始化两个类变量stack1, stack2.
- 代码改进,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的更多相关文章
- 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) -- ...
- 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 ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- Implement Queue by Two Stacks
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
1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...
- lintcode :implement queue by two stacks 用栈实现队列
题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- Java语言基础之方法的设计
开发遵循原则之一: DRY原则:Don't Repeat Yourself(不要重复你自己的代码) 原因:重复意味着维护成本的增大 public static void main(String[] a ...
- git从已有分支拉新分支开发
开发过程中经常用到从master分支copy一个开发分支,下面我们就用命令行完成这个操作: 1. 切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout maste ...
- npm笔记
#执行npm start时是运行的哪个js文件? 打开package.json看看scripts属性中start配置的是什么运行脚本,这里配置的就是你执行npm start时跑的脚本 #设置npm的源 ...
- spark优化设置
->>>配置参数优化 SparkConf sc = new SparkConf().setAppName("com.sp.test.GroupTop3").set ...
- Jedis连接 HelloWorld实现
建一个Maven项目, pom里加下jedis依赖, <dependency> <groupId>redis.clients</groupId> <artif ...
- Cartographer源码阅读(6):LocalTrajectoryBuilder和PoseExtrapolator
LocalTrajectoryBuilder意思是局部轨迹的构建,下面的类图中方法的参数没有画进去. 注意其中的三个类:PoseExtrapolator类,RealTimeCorrelativeSca ...
- day014 模块
# 1.用于多种语言交互 编程语言通用数据 # 内置的 不需要安装 直接导入使用 import json # 导入一个json模块 # dumps loads# dump load 有持久化的功能 # ...
- DAX/PowerBI系列 - 累计总计(Cumulative Total)
DAX/PowerBI系列 - 累计总计(Cumulative Total) 2017/07/23 更新:B列公式(见最后) 难度: ★★☆☆☆(2星) 适用: ★★☆☆☆(2星) 概况: 这个模式普 ...
- 【Assembly】NO.70.EBook.7.Assembly.1.001-【汇编语言 第3版 张爽】- 基础知识
1.0.0 Summary Tittle:[Assembly]NO.70.EBook.7.Assembly.1.001-[汇编语言 第3版 张爽]- 基础知识 Style:Assembly Serie ...
- 解决bootstrap 模态框 数据清除 验证清空
$("#switchModel").on("hidden.bs.modal", function () { $('#ware-form')[0].reset() ...