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.


分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Queue {
    private Stack<Integer> stack1 = new Stack<Integer>();
    private Stack<Integer> stack2 = new Stack<Integer>();
 
    public Queue() {
       // do initialization if necessary
    }
     
    public void push(int element) {
        // write your code here
        while(!stack1.empty()){
            stack2.push(stack1.pop());
        }
        stack1.push(element);
        while(!stack2.empty()){
            stack1.push(stack2.pop());
        }
    }
 
    public int pop() {
        // write your code here
        if(!stack1.empty())
            return stack1.pop();
        else
            return -1;
    }
 
    public int top() {
        // write your code here
        if(!stack1.empty())
            return stack1.peek();
        else
            return -1;
    }
}

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. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

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

  4. LintCode Implement Queue by Two Stacks

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

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

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

  6. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

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

  8. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  9. 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. 润乾报表整合到Tomcat服务器的部署过程

    转载自:http://www.cnblogs.com/avivaye/archive/2012/11/16/2773681.html 使用第三方的报表设计器/服务器来快速的开发报表. 润乾服务器是使用 ...

  2. TCP的三次握手和四次分手

    TCP的三次握手 图解:     • 第一次握手:客户端发送syn包到服务器,并进入syn_send状态,等待服务器进行确认: • 第二次握手:服务器收到客户端的syn包,必须确认客户的SYN,同时自 ...

  3. oss上传文件0字节

    最近使用oss上传文件,不同项目中使用的版本也不同,之前的都能正常上传,最近因需要添加ObjectMetaData属性,扩展了一个方法,发现上传的文件始终是0字节的,最终跟源码发现conntentLe ...

  4. 11-Dockerfile构建镜像

    用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为: FROM ubuntu RUN apt-get update && apt-get install v ...

  5. Python数据可视化的10种技能

    今天我来给你讲讲Python的可视化技术. 如果你想要用Python进行数据分析,就需要在项目初期开始进行探索性的数据分析,这样方便你对数据有一定的了解.其中最直观的就是采用数据可视化技术,这样,数据 ...

  6. Ubuntu16.04使用Tarball安装ntp

    最近在学习linux,看书上例子(鸟哥的linux私房菜 P674),使用Tarball来安装ntp,出了点问题,提示错误,使用 ./configure 来检测程序时,出现如下提示: 提示少了 ope ...

  7. POJ 1417 并查集 dp

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  8. 构建树形结构数据(全部构建,查找构建)C#版

    摘要: 最近在做任务管理,任务可以无限派生子任务且没有数量限制,前端采用Easyui的Treegrid树形展示控件. 一.遇到的问题 获取全部任务拼接树形速度过慢(数据量大约在900条左右)且查询速度 ...

  9. 当Kubernets遇上阿里云 -之七层负载均衡(一).

    我们知道Kubernetes的service只能实现基于4层的负载均衡,无法提供7层之上的许多特性,诸如基于URL的负载均衡,SSL支持,三方授权等等:Ingress可以实现七层负载均衡的许多功能,唯 ...

  10. [笔记] postgresql 流复制(streaming replication)

    基本环境说明: os:FreeBSD 9.3 postgresql version: master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 ...