链接

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. Flume(二) —— 自定义拦截器、Source、Sink

    自定义拦截器 自定义Source 自定义Sink 引入依赖 <dependency> <groupId>org.apache.flume</groupId> < ...

  2. CentOS7离线安装MySQL8.0

    CentOS7离线安装MySQL8.0 卸载软件 rpm -e --nodeps 要卸载的软件包 root@jacky zookeeper]# rpm -e --nodeps java-1.6.0-o ...

  3. Tript协议|伯尔尼公约|著作权|立法宗旨|自动保护|著作权集体管理|

    知识产权 国际条约: Tript协议是国际性公约,<与贸易有关的知识产权协定>(英文:Agreement on Trade-Related Aspects of Intellectual ...

  4. 扩展 ajaxupload.js ,支持客户端判断上传文件的大小

    onSubmit: function(file, extension){}, 修改为 onSubmit: function(file, extension, size){}, if (! (setti ...

  5. TCP、UDP、HTTP与HTTPS

    TCP.UDP.HTTP与HTTPS都是通信协议,在这里首先先介绍一下什么是通信协议. 什么是通信协议? 通信协议(communications protocol)是指双方实体完成通信或服务所必须遵循 ...

  6. Prefix and Suffix

    题目描述 Snuke is interested in strings that satisfy the following conditions: The length of the string ...

  7. 用C语言实现的轴对称变换

    #include<stdio.h> main() { int i,p,n,k,f,c,h,g,w; ][]; ;i<=;i++) { ;p<=;p++) { a[i][p]=i ...

  8. 基于ci框架 修改出来了一个带农历的万年历。

    1这里没有写model:代码一看就懂,没什么负杂地方,就是麻烦一点. 直接control模块的代码: <?php if ( ! defined('BASEPATH')) exit('No dir ...

  9. [LC] 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  10. oracle存储过程随笔

    有时间要把最近总结的存储过程的问题总结一下,先占坑: create or replace procedure test_proc2 as cursor cur is select YWR,YWRZJH ...