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;
    };

  • 相关阅读:
    was控制台误禁用后的恢复启用办法
    Linux升级内核教程(CentOS7)
    ifcfg-eth配置详解(CentOS6)
    CentOS7和CentOS6的区别
    ftp/sftp定时自动上传文件脚本(CentOS)
    AIX安装JDK1.7教程
    PE文件结构解析
    ffmpeg+libmp3lame库源码安装教程(CentOS)
    kafka安装使用教程
    Weblogic禁用SSLv3和RC4算法教程
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5431412.html
Copyright © 2011-2022 走看看