• [LeetCode] 232. Implement Queue using Stacks


    用栈模拟队列。同理参考影子题225题

    题干即是题意,用栈实现队列的几个函数,例子,

    Example:

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

    思路是需要用到两个栈,用first和second表示。以下分别解释一下每个函数的实现方式。

    push() - 将元素加入队列。这没什么可说的,加入到第一个栈first中。时间O(1)

    pop() - 弹出队列的首个元素。因为stack只会弹出最后一个放入的元素,所以只能通过将第一个stack中的元素全部倒入第二个stack中,再pop的方式,得到queue的首个元素。时间O(n)

    peek() - 看一下队列顶端的元素。因为peek的功能几乎跟pop无异,只是pop会弹出首个元素,peek只是看一下,所以做法跟pop几乎相同,也是需要将所有元素都倒入第二个stack中再peek第二个stack的顶端。时间O(1)

    empty() - 检测队列是否为空。只要看一下两个stack是否都为空即可。时间O(1)

    最后分享一个B站非常好的视频讲解,https://www.bilibili.com/video/av86054613/

    时间 - 参考以上

    空间O(n) - 用到两个stack

    JavaScript实现

     1 /**
     2  * Initialize your data structure here.
     3  */
     4 var MyQueue = function () {
     5     this.first = [];
     6     this.second = [];
     7 };
     8 
     9 /**
    10  * Push element x to the back of queue. 
    11  * @param {number} x
    12  * @return {void}
    13  */
    14 MyQueue.prototype.push = function (x) {
    15     this.first.push(x);
    16 };
    17 
    18 /**
    19  * Removes the element from in front of queue and returns that element.
    20  * @return {number}
    21  */
    22 MyQueue.prototype.pop = function () {
    23     // if the second stack is empty, move everything from first to second
    24     if (!this.second.length) {
    25         while (this.first.length) {
    26             this.second.push(this.first.pop());
    27         }
    28     }
    29     return this.second.pop();
    30 };
    31 
    32 /**
    33  * Get the front element.
    34  * @return {number}
    35  */
    36 MyQueue.prototype.peek = function () {
    37     // almost same as pop()
    38     if (!this.second.length) {
    39         while (this.first.length) {
    40             this.second.push(this.first.pop());
    41         }
    42     }
    43     return this.second[this.second.length - 1];
    44 };
    45 
    46 /**
    47  * Returns whether the queue is empty.
    48  * @return {boolean}
    49  */
    50 MyQueue.prototype.empty = function () {
    51     return !this.first.length && !this.second.length;
    52 };
    53 
    54 /**
    55  * Your MyQueue object will be instantiated and called as such:
    56  * var obj = new MyQueue()
    57  * obj.push(x)
    58  * var param_2 = obj.pop()
    59  * var param_3 = obj.peek()
    60  * var param_4 = obj.empty()
    61  */

    Java实现

     1 class MyQueue {
     2     Stack<Integer> s1;
     3     Stack<Integer> s2;
     4 
     5     /** Initialize your data structure here. */
     6     public MyQueue() {
     7         s1 = new Stack<>();
     8         s2 = new Stack<>();
     9     }
    10 
    11     /** Push element x to the back of queue. */
    12     public void push(int x) {
    13         s1.push(x);
    14     }
    15 
    16     /** Removes the element from in front of queue and returns that element. */
    17     public int pop() {
    18         if (!s2.isEmpty()) {
    19             return s2.pop();
    20         } else {
    21             while (!s1.isEmpty()) {
    22                 s2.push(s1.pop());
    23             }
    24             return s2.pop();
    25         }
    26     }
    27 
    28     /** Get the front element. */
    29     public int peek() {
    30         if (!s2.isEmpty()) {
    31             return s2.peek();
    32         } else {
    33             while (!s1.isEmpty()) {
    34                 s2.push(s1.pop());
    35             }
    36             return s2.peek();
    37         }
    38     }
    39 
    40     /** Returns whether the queue is empty. */
    41     public boolean empty() {
    42         return s1.isEmpty() && s2.isEmpty();
    43     }
    44 }
    45 
    46 /**
    47  * Your MyQueue object will be instantiated and called as such:
    48  * MyQueue obj = new MyQueue();
    49  * obj.push(x);
    50  * int param_2 = obj.pop();
    51  * int param_3 = obj.peek();
    52  * boolean param_4 = obj.empty();
    53  */

    LeetCode 题目总结

  • 相关阅读:
    python之-- 异常
    实现Asp.Net Mvc4多级Views目录
    MVC控制下输出图片、javascript与json格式
    公共增删改查(MVC+三层架构)
    工厂方法模式
    简单工厂模式
    单例模式
    JavaScript正则表达式
    JavaScript对象与数组
    JavaScript数组排序
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12406589.html
Copyright © 2020-2023  润新知