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

2020年2月1日08:53:07算法 LeetCode评论51阅读模式

题目链接

https://leetcode-cn.com/problems/implement-queue-using-stacks/文章源自随机的未知-https://sjdwz.com/11122.html

题目描述

使用栈实现队列的下列操作:文章源自随机的未知-https://sjdwz.com/11122.html

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

示例:文章源自随机的未知-https://sjdwz.com/11122.html

MyQueue queue = new MyQueue(); queue.push(1); queue.push(2);
queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false文章源自随机的未知-https://sjdwz.com/11122.html

思路

使用两个栈来完成操作, 首先全部进入第一个栈,再全部进入第二个栈,用图来演示一下: 首先进入栈1;文章源自随机的未知-https://sjdwz.com/11122.html

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

然后出栈1,入栈2; LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解 再出栈2。 LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解文章源自随机的未知-https://sjdwz.com/11122.html

由图可以知道,用两个栈即可完成队列的操作;文章源自随机的未知-https://sjdwz.com/11122.html

分为下面三种情况文章源自随机的未知-https://sjdwz.com/11122.html

  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();
    }
}

欢迎关注

扫下方二维码即可关注:,微信公众号:随机的未知 LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解文章源自随机的未知-https://sjdwz.com/11122.html

文章源自随机的未知-https://sjdwz.com/11122.html
欢迎关注本站微信公众号:随机的未知 如果喜欢本文,欢迎点赞,收藏,转发,打赏。
  • 本文由 发表于 2020年2月1日08:53:07
  • 转载请注明:来源:随机的未知 本文链接https://sjdwz.com/11122.html
算法

详解堆排序

什么是堆 堆首先是一个完全二叉树,堆分为大顶堆和小顶堆; 大顶堆 : 每个节点的值大于或等于其左右孩子节点的值,称为大顶堆。 小顶堆同理就是每个节点的值小于或等于其左右孩子节点的值。 注意: 每个节点...
算法

详解基数排序

基本思想 基数排序的思想是将整数按位数切割成不同的数字,然后按每个位数分别比较从而得到有序的序列。 例子 本文以数组中元素均为正整数来演示思想。 给定一个数组 arr = < 6, 56, 89 , ...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定