• 232 Implement Queue using Stacks 用栈来实现队列


    使用栈来实现队列的如下操作:

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

    你只能使用标准的栈操作-- 也就是只有push to top, peek/pop from top, size, 和 is empty 操作是可使用的。
    你所使用的语言也许不支持栈。你可以使用 list 或者 deque (双端队列)来模拟一个栈,只要你仅使用栈的标准操作就可以。
    假设所有操作都是有效的,比如 pop 或者 peek 操作不会作用于一个空队列上。

    详见:https://leetcode.com/problems/implement-queue-using-stacks/description/

    Java实现:

    方法一:

    class MyQueue {
    
        /** Initialize your data structure here. */
        private Stack<Integer> stk;
        public MyQueue() {
            stk=new Stack<Integer>();
        }
        
        /** Push element x to the back of queue. */
        public void push(int x) {
            stk.add(0,x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            return stk.pop();
        }
        
        /** Get the front element. */
        public int peek() {
            return stk.peek();
        }
        
        /** Returns whether the queue is empty. */
        public boolean empty() {
            return stk.isEmpty();
        }
    }
    
    /**
     * Your MyQueue object will be instantiated and called as such:
     * MyQueue obj = new MyQueue();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.peek();
     * boolean param_4 = obj.empty();
     */
    

    方法二:

    class MyQueue {
    
        /** Initialize your data structure here. */
        private Stack<Integer> stkPush;
        private Stack<Integer> stkPop;
        public MyQueue() {
            stkPush=new Stack<Integer>();
            stkPop=new Stack<Integer>();
        }
        
        /** Push element x to the back of queue. */
        public void push(int x) {
            stkPush.push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            if(stkPop.isEmpty()){
                while(!stkPush.isEmpty()){
                    stkPop.push(stkPush.pop());
                }
            }
            return stkPop.pop();
        }
        
        /** Get the front element. */
        public int peek() {
            if(stkPop.isEmpty()){
                while(!stkPush.isEmpty()){
                    stkPop.push(stkPush.pop());
                }
            }
            return stkPop.peek();
        }
        
        /** Returns whether the queue is empty. */
        public boolean empty() {
            return stkPush.isEmpty()&&stkPop.isEmpty();
        }
    }
    
    /**
     * Your MyQueue object will be instantiated and called as such:
     * MyQueue obj = new MyQueue();
     * obj.push(x);
     * int param_2 = obj.pop();
     * int param_3 = obj.peek();
     * boolean param_4 = obj.empty();
     */
    

     C++实现:

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

      

  • 相关阅读:
    Redis源码解析:28集群(四)手动故障转移、从节点迁移
    Redis源码解析:27集群(三)主从复制、故障转移
    Redis源码解析:26集群(二)键的分配与迁移
    centos 6.5 安装composer
    Centos安装php高版本
    CentOS快速搭建LAMP环境
    封装类似thinkphp连贯操作数据库的Db类(简单版)。
    php封装pdo操作数据的工具类
    php中使用mysqli和pdo扩展,测试mysql数据库的执行效率。
    php中使用mysqli和pdo扩展,测试连接mysql数据库的效率。
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8758959.html
Copyright © 2020-2023  润新知