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. HTML基本代码教学,第三天

    HTML 今天由于个人情况,身体不适,但是为了大家的学习进度,咱们以纯文字得形式来简单了解下今天的学习内容 今儿咱们来了解下表单 <form id=" "  name=&qu ...

  2. 【转】glumer Appium + Python环境搭建(移动端自动化)

    最近整理了一下自动化的东西,好久没搭建环境又踩了不少坑,appium的环境搭建比较繁琐,好多同行估计都在环境上被卡死了.分享一下~~ 一.安装JDK,配置JDK环境    百度搜索下载就行,这里分享一 ...

  3. InTelliJ 字体调整

    Java IDE 工具InTelliJ 调整字体大小 1.File -> Settings 2.左上的搜索框中输入 font. 等待自动查找结果. 3.修改size 大小

  4. Centos6升级至openssh-7.5p1

    最近公司有几台服务器需要搬至甲方(政府单位),所以在安装服务时用的是16年的openssh7.3pl, 今天通知我们有漏洞,需要再一次升级,看到官方文档上版本已升级至7.5,所以干脆直接搞7.5 具体 ...

  5. hbase Problem binding to node1/192.168.1.13:16020 : 地址已在使用

    这是hbase 从0.9.x升级到1.x后HMaster与HRegionServer端口冲突问题 在hbase升级到1.0.0版本后,默认端口做了改动.其中16020端口是hmaster服务和hreg ...

  6. Beta阶段中间产物

    空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...

  7. 【Alpha】阶段第三次Scrum Meeting

    [Alpha]阶段第三次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 更新评论接口 获取课程评论接口 赵智源 编写脚本实现持续集成 整合前端进行部署 肖萌威 ...

  8. Team Work Ⅲ

    Regal-Lighting团队设计 分工思考 本次大作业我的分工定位是:Unit及子类,主要设计实现建筑类的功能. 在上一篇博客我介绍了我的继承方案和接口设定,这一篇粗略的介绍一下实现部分 Defe ...

  9. sql高级主题资料(网络复制)

    SQL Server 常用高级语法笔记   自从用了EF后很少写sql和存储过程了,今天需要写个比较复杂的报告,翻出了之前的笔记做参考,感觉这个笔记还是很有用的,因此发出来和大家分享. 1.case. ...

  10. c# Webservice技术整理

    因为平常项目中使用webservice比较少,然后就将本来不太熟悉的webservice给忘记掉了.所以再次整理如下: 百度搜索关键词 :c# webservice 1. 联接地址: http://w ...