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,重新将队尾的元素拿出来加到后面。
  • 相关阅读:
    Java程序员:一整个项目的具体开发流程介绍
    JAVA常用API整理
    Java开发人员必知必会的20种常用类库和API
    SpringBoot_web开发【实验】-员工列表-链接高亮&列表完成
    luogu P1754 球迷购票问题 |动态规划
    luogu P1566 加等式 |背包问题方案数
    luogu P1564 膜拜 |动态规划
    luogu P1509 找啊找啊找GF |背包
    P1474 货币系统 Money Systems |背包方案数
    cloudera安装报错 socket.gaierror: [Errno -2] Name or service not known
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4580936.html
Copyright © 2011-2022 走看看