zoukankan      html  css  js  c++  java
  • Implement Stack using Queues

    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.

    思路:

      使用两个队列来回交替的来pop

    我的代码:

    class MyStack {
        Queue<Integer> one = new LinkedList<Integer>();
        Queue<Integer> two = new LinkedList<Integer>();
        public void push(int x) {
            if(one.isEmpty())
                two.offer(x);
            else
                one.offer(x);
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            if(one.isEmpty())
            {
                while(two.size() != 1)
                    one.offer(two.poll());
                two.poll();
            }
            else
            {
                while(one.size() != 1)
                    two.offer(one.poll());
                one.poll();
            }
        }
        // Get the top element.
        public int top() {
            if(one.isEmpty())
            {
                while(two.size() != 1)
                    one.offer(two.poll());
                int last = two.poll();
                one.offer(last);
                return last;
            }
            else
            {
                while(one.size() != 1)
                    two.offer(one.poll());
                int last = one.poll();
                two.offer(last);
                return last;
            }
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return one.isEmpty() && two.isEmpty();
        }
    }
    View Code

    他人代码:

    class MyStack {
        Queue<Integer> q = new LinkedList<Integer>();
    
        // Push element x onto stack.
        public void push(int x) {
            q.add(x);
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            int size = q.size();
            for(int i = 1; i < size; i++)
                q.add(q.remove());
            q.remove();
        }
    
        // Get the top element.
        public int top() {
            int size = q.size();
            for(int i = 1; i < size; i++)
                q.add(q.remove());
            int ret = q.remove();
            q.add(ret);
            return ret;
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return q.isEmpty();        
        }
    }
    View Code

    学习之处:

    • 别人用了一个队列就解决了问题,我用了两个队列,浪费了空间,别人代码的精华之处在于for(int i = 1; i < size; i++) q.add(q.remove());通过size,重新将队尾的元素拿出来加到后面。
  • 相关阅读:
    Centos8安装MySQL8(社区版)
    DateTime.Now 在.netcore下的格式问题
    HP Socket FAQ
    docker基本操作
    Win10-Docker和VMware运行环境冲突解决办法
    Centos8安装docker-compose
    .net5 RSA
    密码规则之数字、小写、大写、特殊字符,至少满足3个
    .net5 应用程序启动和停止事件
    MySQL中国省市区数据表
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4580936.html
Copyright © 2011-2022 走看看