zoukankan      html  css  js  c++  java
  • Leetcode题目: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).

    题目解答:题目中要求使用队列来实现栈的功能。其中,对于队列只可以使用它的push,pop,size,empty操作。

    代码为:

    class Stack {
    public:
        // Push element x onto stack.
        void push(int x) {
            if(q1.empty())
            {
                q2.push(x);
            }
            else
            {
                q1.push(x);
            }
        }

        // Removes the element on top of the stack.
        void pop() {
            if(q1.empty())
            {
                while(q2.size() > 1)
                {
                    q1.push(q2.front());
                    q2.pop();
                }
                if(q2.size() == 1)
                    q2.pop();
            }
            else
            {
                while(q1.size() > 1)
                {
                    q2.push(q1.front());
                    q1.pop();
                }
                if(q1.size() == 1)
                    q1.pop();
            }
        }

        // Get the top element.
        int top() {
            int tmp = -1;
            if(q1.empty())
            {
                while(q2.size() > 1)
                {
                    q1.push(q2.front());
                    q2.pop();
                }
                if(q2.size() == 1)
                {
                    tmp = q2.front();
                    q1.push(q2.front());
                    q2.pop();
                }
            }
            else
            {
                while(q1.size() > 1)
                {
                    q2.push(q1.front());
                    q1.pop();
                }
                if(q1.size() == 1)
                {
                    tmp = q1.front();
                    q2.push(q1.front());
                    q1.pop();
                }
            }
            return tmp;
        }

        // Return whether the stack is empty.
        bool empty() {
            return q1.empty() && q2.empty();
        }
    private:
        queue<int> q1;
        queue<int> q2;
    };

  • 相关阅读:
    Celery
    Xadmin的基本使用 补充:admin控制user字段
    Python pip换源 创建虚拟环境 luffy项目配置(数据库bug)
    gitignore 文件 刷新
    django python mange.py runserver 源码
    leetcode125. 验证回文串 python 简单
    leetcode1028 从先序遍历还原二叉树 python 100%内存 一次遍历
    fiddler
    xpath
    linux常用命令
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5431412.html
Copyright © 2011-2022 走看看