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. VS2013 Ctrl+Shift+F 没反应

    快捷键设定与搜狗输入法中文繁体切换冲突,搜狗输入法管理设置中关掉就好.

  2. 编译安装centos7 php7.2 mysql5.7 nginx1.9.9

    2018年3月12日 14:09:39 注意时效 centos7 网卡 cd /etc/sysconfig/network-scripts/ TYPE=Ethernet PROXY_METHOD=no ...

  3. Eclispe最常用的几个快捷键

    熟练使用快捷键可以在很大程度上提高我们的工作效率,Eclipse的快捷键很多,但是常用的也就那么几个,下面说下Eclispe最常用的几个快捷键: Eclipse的快捷键组合可在Eclipse按下ctr ...

  4. Pycharm调试:进入调用函数后返回

    在菜单栏的view中勾选toolbar,然后点击工具栏中左箭头返回到调用函数处.

  5. JDBC事务(一)

    package cn.sasa.tran01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.P ...

  6. bounds的应用

    frame是参考父view的坐标系来设置自己左上角的位置.设置bounds可以修改自己坐标系的原点位置,进而影响到其“子view”的显示位置.   向上滚动scrollview,我们就不断增加scro ...

  7. xutils android studio引用问题

    然后rebuild--->关闭项目-->重启,ok public class MyApplication extends Application { @Override public vo ...

  8. C#:进程

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. 浏览器 User-Agent相关知识

    文章引用链接 http://www.360doc.com/content/12/1012/21/7662927_241124973.shtml

  10. OpenStack-RabbitMQ-获取vm、磁盘、网络设备的状态变化

    需求 及时知道vm,硬盘,镜像,网络设备.负载均衡器状态变化 分析 Dashboard中也是通过定时使用ajax调用API来获取虚拟机的状态信息的定时轮训的方式过于被动 解决方案 共用rabbitmq ...