题目链接

https://leetcode-cn.com/problems/implement-queue-using-stacks/

题目描述

使用栈实现队列的下列操作:

push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。

示例:

MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

思路

使用两个栈来完成操作, 首先全部进入第一个栈,再全部进入第二个栈,用图来演示一下:
首先进入栈1;

然后出栈1,入栈2;

再出栈2。

由图可以知道,用两个栈即可完成队列的操作;

分为下面三种情况

  1. Stack_1空,Stack_2有元素,这时push()操作让Stack_1进行push();pop()操作,只需让Stack_2进行pop();peek()操作,也只需让Stack_2进行peek()就可以了;这时队列不为空;
  2. Stack_1不空,Stack_2空,这时push()操作让Stack_1进行push();pop()操作需要将Stack_1的所有元素进入Stack_2,Stack_2进行pop();peek()操作,也只需要将Stack_1的所有元素进入Stack_2,再让Stack_2进行peek()就可以了;这是队列不为空;
  3. Stack_1空,Stack_2也为空,push()操作让Stack_1进行push()即可,pop()和push()无法完成,队为空。

代码

import java.util.Stack;

class MyQueue {

    //初始化栈1和栈2
    private Stack<Integer> Stack_1;
    private Stack<Integer> Stack_2;
    /** Initialize your data structure here. */
    public MyQueue() {
        Stack_1 = new Stack<>();
        Stack_2 = new Stack<>();
    }
    //进入第一个栈
    /** Push element x to the back of queue. */
    public void push(int x) {
        Stack_1.push(x);
    }     /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        //如果栈2是空的
        if(Stack_2.isEmpty()){
            //将栈1的所有元素入栈2
            while(!Stack_1.isEmpty()){
                Stack_2.push(Stack_1.pop());
            }
        }
        if (!Stack_2.isEmpty()) {
            return Stack_2.pop();
        }
        throw new RuntimeException("MyQueue空了!");
    }     /** Get the front element. */
    public int peek() {
        //如果栈2是空的
        if(Stack_2.isEmpty()){
            //将栈1的所有元素入栈2
            while(!Stack_1.isEmpty()){
                Stack_2.push(Stack_1.pop());
            }
        }         if (!Stack_2.isEmpty()) {
            return Stack_2.peek();
        }
        throw new RuntimeException("MyQueue空了!");     }     /** Returns whether the queue is empty. */
    public boolean empty() {
        return Stack_1.isEmpty() && Stack_2.isEmpty();
    }
}

欢迎关注

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

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

  1. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  2. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  3. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. leetcode刷题记录——栈和队列

    题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...

  5. LeetCode232 Implement Queue using Stacks Java 题解

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

  6. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

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

  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. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

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

随机推荐

  1. 大集合List分为多个子集合

    批量插入时如果一次插入的对象过多会导致超过mysql限定sql长度,通过命令查看 show VARIABLES like 'max_allowed_packet' ,如果数据太多,就将大集合List分 ...

  2. [JSOI2019]神经网络(树形DP+容斥+生成函数)

    首先可以把题目转化一下:把树拆成若干条链,每条链的颜色为其所在的树的颜色,然后排放所有的链成环,求使得相邻位置颜色不同的排列方案数. 然后本题分为两个部分:将一棵树分为1~n条不相交的链的方案数:将这 ...

  3. Equal Cut

    Equal Cut 题目描述 Snuke has an integer sequence A of length N. He will make three cuts in A and divide ...

  4. DataSet,DataTable排序(转载)

    DataSet,DataTable排序   关于对已经绑定的DataSet的排序的问题: DataSet ds=new DataSet();DataView dv=new DataView();dv. ...

  5. mysql自定义函数统计订单状态:GET_ORDER_STATUS()

    DELIMITER $$ USE `local_hnyz`$$ DROP FUNCTION IF EXISTS `GET_ORDER_STATUS`$$ CREATE DEFINER=`root`@` ...

  6. 系统学习Javaweb9----BootStrap1

    学习内容: 1.BootStrap的简述 2.BootStrap环境搭建 3.BootStrap环境搭建-基本模板创建 4.BootStrap环境搭建-基本模板讲解 5.BootStrap布局容器 6 ...

  7. 扩增|feather evolution

    Wool vs feather 扩增方法:1.Gene Duplication2.Genome Duplication3.Cluster 哺乳动物毛发和鸟类的羽毛,都来自于角蛋白. 羽毛进化图 DNA ...

  8. ready vs onload

    1 ready事件:当DOM载入就绪,可以查询,操纵时绑定一个要执行的函数.它可以极大地提高web应用程序的响应速度. 2  onload事件:js中的方法,网页的所有元素.图片全部加载完毕才执行这个 ...

  9. mysql查找json格式列的指定字段值

    SELECT json_extract(字段名,'$.json结构') FROM 表名;如果json里有双引号,那这样取出来的数据也带双引号,要去掉就使用REPLACE函数 例如t_submit_an ...

  10. MyEclipse10安装properties文件插件

    安装步骤 1.下载PropertiesEditor插件 http://propedit.sourceforge.jp/index_en.html 2.解压出features.plugins文件 3.在 ...