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. MySQL中kill掉所有表的进程

    同事打电话告诉我用户数据库挂掉了. 我起床看一下进程列表. mysql>show processlist; 出来哗啦啦好几屏幕的, 没有一千也有几百条, 查询语句把表锁住了, 赶紧找出第一个Lo ...

  2. Numpy学习

    决定陆陆续续写一些Numpy的例子.. 1. 如果想表示e的x次,就可以这样用,下面直接写一个sigmod函数: def sigmoid(z): return 1 / (1 + np.exp(-z)) ...

  3. 转 基于Excel参数化你的Selenium2测试

    转载:https://blog.csdn.net/zhusongziye/article/details/80100375 前言 今天我们就如何使用xlrd模块来进行python selenium2 ...

  4. vue构造器的内容

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

  6. 20165317Java实验五 网络编程与安全

    实验五 网络编程与安全 一.中缀转后缀 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 结对实现中缀表达式转后缀表达式的功能 MyBC.ja ...

  7. java框架之Hibernate(2)-持久化类&主键生成策略&缓存&事务&查询

    持久化类 概述 持久化:将内存中的对象持久化到数据库中的过程就是持久化.Hibernate 就是用来进行持久化的框架. 持久化类:一个 Java 对象与数据库的表建立了映射关系,那么这个类在 Hibe ...

  8. java框架之Hibernate(3)-一对多和多对多关系操作

    一对多 例:一个班级可以有多个学生,而一个学生只能属于一个班级. 模型 package com.zze.bean; import java.util.HashSet; import java.util ...

  9. python 转换容量单位 实现ls -h功能

    功能1 把字节转换自适应转为其他单位(ls -h),超过1024投入高一级的区间,不足1024投入本级区间,如1000K是一个合理值,1030K就应该转换为1M,2050K应该转换为2M 功能2 把其 ...

  10. Qt 拷贝内容到粘贴板 || 获取粘贴板内容

    QString source = ui->textEdit_code->toPlainText(); QClipboard *clipboard = QApplication::clipb ...