zoukankan      html  css  js  c++  java
  • 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues.

    push(x) -- Push element x onto stack.
    pop() -- Removes the element on top of the stack.
    top() -- Get the top element.
    empty() -- Return whether the stack is empty.
    Notes:
    You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
    Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
    You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

    详见:https://leetcode.com/problems/implement-stack-using-queues/description/

    Java实现:

    方法一:两个队列实现,始终保持一个队列为空即可

    class MyStack {
        private Queue<Integer> queue1;
        private Queue<Integer> queue2;
    
        /** Initialize your data structure here. */
        public MyStack() {
            queue1=new LinkedList<Integer>();
            queue2=new LinkedList<Integer>();
        }
        
        /** Push element x onto stack. */
        public void push(int x) {
            if(queue1.isEmpty()){
                queue1.offer(x);
                while(!queue2.isEmpty()){
                    queue1.offer(queue2.poll());
                }
            }else if(queue2.isEmpty()){
                queue2.offer(x);
                while(!queue1.isEmpty()){
                    queue2.offer(queue1.poll());
                }
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            if(queue1.isEmpty()){
                return queue2.poll();
            }else{
                return queue1.poll();
            }
        }
        
        /** Get the top element. */
        public int top() {
            if(queue1.isEmpty()){
                return queue2.peek();
            }else{
                return queue1.peek();
            }
        }
        
        /** Returns whether the stack is empty. */
        public boolean empty() {
            return queue1.isEmpty()&&queue2.isEmpty();
        }
    }
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack obj = new MyStack();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.top();
     * boolean param_4 = obj.empty();
     */
    

    方法二:一个队列实现栈

    class MyStack {
        private Queue<Integer> queue;
    
        /** Initialize your data structure here. */
        public MyStack() {
            queue=new LinkedList<Integer>();
        }
        
        /** Push element x onto stack. */
        public void push(int x) {
            queue.offer(x);
            for(int i=0;i<queue.size()-1;++i){
                queue.offer(queue.poll());
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            return queue.poll();
        }
        
        /** Get the top element. */
        public int top() {
            return queue.peek();
        }
        
        /** Returns whether the stack is empty. */
        public boolean empty() {
            return queue.isEmpty();
        }
    }
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack obj = new MyStack();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.top();
     * boolean param_4 = obj.empty();
     */
    

    C++实现:

    方法一:两个队列实现,始终保持一个队列为空即可

    class MyStack {
    public:
        /** Initialize your data structure here. */
        MyStack() {
            
        }
        
        /** Push element x onto stack. */
        void push(int x) {
            if(que1.empty())
            {
                que1.push(x);
                while(!que2.empty())
                {
                    que1.push(que2.front());
                    que2.pop();
                }
            }
            else if(que2.empty())
            {
                que2.push(x);
                while(!que1.empty())
                {
                    que2.push(que1.front());
                    que1.pop();
                }
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        int pop() {
            int val;
            if(que1.empty())
            {
                val=que2.front();
                que2.pop();
            }
            else if(que2.empty())
            {
                val=que1.front();
                que1.pop();
            }
            return val;
        }
        
        /** Get the top element. */
        int top() {
            if(que1.empty())
            {
                return que2.front();
            }
            else if(que2.empty())
            {
                return que1.front();
            }
        }
        
        /** Returns whether the stack is empty. */
        bool empty() {
            return que1.empty()&&que2.empty();
        }
    private:
        queue<int> que1;
        queue<int> que2;
    };
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack obj = new MyStack();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.top();
     * bool param_4 = obj.empty();
     */
    

    方法二:一个队列实现栈

    class MyStack {
    public:
        /** Initialize your data structure here. */
        MyStack() {
            
        }
        
        /** Push element x onto stack. */
        void push(int x) {
            que.push(x);
            for(int i=0;i<que.size()-1;++i)
            {
                que.push(que.front());
                que.pop();
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        int pop() {
            int val=que.front();
            que.pop();
            return val;
        }
        
        /** Get the top element. */
        int top() {
            return que.front();
        }
        
        /** Returns whether the stack is empty. */
        bool empty() {
            return que.empty();
        }
    private:
        queue<int> que;
    };
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack obj = new MyStack();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.top();
     * bool param_4 = obj.empty();
     */
    
  • 相关阅读:
    ActiveMQ的作用总结(应用场景及优势)
    深入剖析ThreadLocal实现原理以及内存泄漏问题
    Java ThreadLocal 原理分析
    关于iview ui的"Page分页"组件的使用
    使用TortoiseGit操作分支的创建与合并
    乐观锁与悲观锁的区别
    Linux下如何安装MongoDB及配置
    centos8安装图解(超详细教程)
    SpringBoot整合MongoDB
    PPT学习---醍醐灌顶!你对PPT的理解过时了!
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8758004.html
Copyright © 2011-2022 走看看