zoukankan      html  css  js  c++  java
  • 【LeetCode】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 must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is emptyoperations are valid.
      • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
      • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

    题解:

    用一个辅助栈逆序,不过每push一个元素就要反转整个栈,非常耗时,不可取

    Solution 1 

    class MyQueue {
    public:
        /** Initialize your data structure here. */
        MyQueue() {
            
        }
        
        /** Push element x to the back of queue. */
        void push(int x) {
            stack<int> tmp;
            while(!s.empty()){
                tmp.push(s.top());
                s.pop();
            }
            s.push(x);
            while(!tmp.empty()){
                s.push(tmp.top());
                tmp.pop();
            }
        }
        
        /** Removes the element from in front of queue and returns that element. */
        int pop() {
            int tmp =  s.top();
            s.pop();
            return tmp;
        }
        
        /** Get the front element. */
        int peek() {
            return s.top();
        }
        
        /** Returns whether the queue is empty. */
        bool empty() {
            return s.empty();
        }
        stack<int> s;
    };

    设两个栈,一个只能push,一个只能pop。有些情况需要注意,只有s2栈空了,才能把s1栈元素push进s2,而且一定要全部push进s2。另外在返回首元素时,有时候是刚开始push进s1,还没有pop操作(也就是说s1的元素还没有push进s2,此时s2为空,但是s1不为空),那么就要和pop操作一样要将栈1元素全部push进栈2。

    Solution 2

    class MyQueue {
    public:
        /** Initialize your data structure here. */
        MyQueue() {
            
        }
        
        /** Push element x to the back of queue. */
        void push(int x) {
            s1.push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        int pop() {
            if(s2.empty() && s1.empty()){
                return INT_MIN;
            } else if(s2.empty()){
                while(!s1.empty()){
                    int tmp = s1.top();
                    s1.pop();
                    s2.push(tmp);
                }
            }
            int tmp = s2.top();
            s2.pop();
            return tmp;
        }
        
        /** Get the front element. */
        int peek() {
            if(!s2.empty())
                return s2.top();
            while(!s1.empty()){
                    int tmp = s1.top();
                    s1.pop();
                    s2.push(tmp);
            }
            return s2.top();
        }
        
        /** Returns whether the queue is empty. */
        bool empty() {
            return s1.empty() && s2.empty();
        }
        stack<int> s1, s2;
    };
  • 相关阅读:
    手把手教你利用create-nuxt-app脚手架创建NuxtJS应用
    初识NuxtJS
    webpack打包Vue应用程序流程
    用选择器代替表格列的筛选功能
    Element-UI
    Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]
    Sparse Principal Component Analysis via Rotation and Truncation
    Generalized Power Method for Sparse Principal Component Analysis
    Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)
    Truncated Power Method for Sparse Eigenvalue Problems
  • 原文地址:https://www.cnblogs.com/Atanisi/p/7507036.html
Copyright © 2011-2022 走看看