链接

https://leetcode-cn.com/problems/implement-stack-using-queues/

思路

首先演示push()操作;
将元素依次进入队1,进入时用top元素保存当前进入的元素;
如下图:

                                  push操作的演示

然后演示pop()操作;
先将除队1中的最后一个元素出队并进入队2,入队2时用top存储入队元素;
再将队列1和队列2进行互换即可。
如下图:

                                    pop操作的演示

代码

import java.util.LinkedList;
import java.util.Queue; class MyStack {     private Queue<Integer> queue_1 = new LinkedList<>();
    private Queue<Integer> queue_2 = new LinkedList<>();
    private int top;
    /** Initialize your data structure here. */
    public MyStack() {     }     /** Push element x onto stack. */
    public void push(int x) {
        queue_1.add(x);
        top = x;
    }     /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(empty()){
            throw new RuntimeException("MyStack空了!");
        }
        while(queue_1.size() > 1){
            top = queue_1.remove();
            queue_2.add(top);
        }
        int last_ele = queue_1.remove();
        Queue<Integer> queue_temp = queue_1;
        queue_1 = queue_2;
        queue_2 = queue_temp;
        return last_ele;
    }     /** Get the top element. */
    public int top() {
        if(empty()){
            throw new RuntimeException("MyStack空了!");
        }
        return top;
    }     /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue_1.isEmpty() && queue_2.isEmpty() ;
    }
} /**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

欢迎关注

扫下方二维码即可关注:,微信公众号:code随笔

                               

LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解的更多相关文章

  1. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  2. [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. Leetcode 225 两个队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

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

  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. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  8. 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) -- ...

  9. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

随机推荐

  1. vue 项目在scope中使用@import引入css ,作用域是全局

    有时候引入第三ui插件,修改样式 时候,需要再单独定义style标签,才有效果,可是会影响全局影响全局,如下所示 加上/deep/,就可以了,

  2. 48)PHP,工厂模式

    为啥需要工厂模式啊: (原来是生产类的工具——————————) 工厂类的代码格式: <?php class factory{ //Instance表示“实例”,“对象” static func ...

  3. E. Alice and the Unfair Game(推导线段树)

    题:https://codeforces.com/contest/1236/problem/E 粗自:https://www.cnblogs.com/YSFAC/p/11715522.html #in ...

  4. python3多线程应用详解(第四卷:图解多线程中LOCK)

    先来看下图形对比: 发现没有这种密集型计算的任务中,多线程没有穿行的速率快,原因就是多线程在线程切换间也是要耗时的而密集型计算任务执行时几乎没以偶IO阻塞,这样你说谁快

  5. Qt QLabel show 显示图像、填充、缩放

    主要成员函数: 1.void setText(QString); //设置label框内的文本. 2.void hide(); //隐藏label框. 3.void setBuddy(QWidget* ...

  6. python往mysql数据库中写入数据和更新插入数据

    本文链接:https://blog.csdn.net/Mr__lqy/article/details/85719603 1. 连接mysql import pymysql db = pymysql.c ...

  7. python-django-redis拒绝连接问题解决_20191121

    今天安装fastdfs的时候,发现最好固定虚拟机的ip, 固定了ip之后,发现使用Windows中的pycharm连接redis的时候,总是拒绝连接,找了很多的办法都不行,有点慌, 但是不能慌,现在要 ...

  8. Hypothesis Tests for One Population Mean When σ Is Unknown|other

    9.5 Hypothesis Tests for One Population Mean When σ Is Unknown 使用t分布: What If the Assumptions Are No ...

  9. 图形学创世纪——写在SIGGRAPH 40年的边上

    40年的边上" title="图形学创世纪--写在SIGGRAPH 40年的边上"> 前言: SIGGRAPH是由ACM SIGGRAPH(美国计算机协会计算机图形 ...

  10. deeplearning.ai 卷积神经网络 Week 2 卷积神经网络经典架构

    1. Case study:学习经典网络的原因是它们可以被迁移到其他任务中. 1.1)几种经典的网络: a)LeNet-5(LeCun et al., 1998. Gradient-based lea ...