LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

题目链接

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

然后出栈1,入栈2;

出栈1入栈2
再出栈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随笔

微信公众号:code随笔

原文地址:https://www.cnblogs.com/nicaicai/p/12247510.html

时间: 2024-08-29 07:18:30

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

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

题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in

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

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

[LeetCode] 232. Implement Queue using Stacks Java

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

232. Implement Queue using Stacks Java Solutions

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

LeetCode232 Implement Queue using Stacks Java 题解

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

Leetcode题解——数据结构之栈和队列

1. 用栈实现队列 2. 用队列实现栈 3. 最小值栈 4. 用栈实现括号匹配 5. 数组中元素与下一个比它大的元素之间的距离 6. 循环数组中比当前元素大的下一个元素 1. 用栈实现队列 232. Implement Queue using Stacks (Easy) 栈的顺序为后进先出,而队列的顺序为先进先出.使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序. class MyQueue { private

【LeetCode】232. Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i