LeetCode OJ 之 Implement Stack using Queues(使用队列实现栈)

来源:互联网 发布:淘宝的直通车在哪里 编辑:程序博客网 时间:2024/06/15 03:33

题目:

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 backpeek/pop from frontsize, 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).

思路:

1、使用两个队列实现。参考:http://blog.csdn.net/u012243115/article/details/45440645。

2、使用一个队列实现。

代码1:

class Stack {public:    queue<int> q1;    queue<int> q2;    // Push element x onto stack.    void push(int x)     {        if(!q2.empty())            q2.push(x);        else            q1.push(x);    }    // Removes the element on top of the stack.    void pop()     {        if(q1.empty() && !q2.empty())        {            size_t len = q2.size();            while(len != 1)            {                q1.push(q2.front());                q2.pop();                len--;            }            q2.pop();            return;        }        else        {            if(q2.empty() && !q1.empty())            {                size_t len2 = q1.size();                while(len2 != 1)                {                    q2.push(q1.front());                    q1.pop();                    len2--;                }                q1.pop();                return;            }        }                    }    // Get the top element.    int top()     {        if(q1.empty() && !q2.empty())        {            size_t len = q2.size();            while(len != 1)            {                q1.push(q2.front());                q2.pop();                len--;            }            int tmp = q2.front();            q2.pop();            q1.push(tmp);            return tmp;        }        else        {            if(q2.empty() && !q1.empty())            {                size_t len2 = q1.size();                while(len2 != 1)                {                    q2.push(q1.front());                    q1.pop();                    len2--;                }               int tmp2 = q1.front();               q1.pop();               q2.push(tmp2);               return tmp2;            }        }    }    // Return whether the stack is empty.    bool empty()     {        return q1.empty() && q2.empty();    }};

代码2:

class Stack {public:    queue<int> q;    // Push element x onto stack.    void push(int x) {        q.push(x);    }    // Removes the element on top of the stack.    void pop()     {        if(q.empty())            return;        int len = q.size();        for(int i = 1 ; i < len ; i++)        {            int tmp = q.front();            q.pop();            q.push(tmp);        }        q.pop();    }    // Get the top element.    int top()     {        return q.back();    }    // Return whether the stack is empty.    bool empty()     {        return q.empty();    }};

代码3:

不使用queue的back

class Stack {public:    queue<int> q;    // Push element x onto stack.    void push(int x) {        q.push(x);    }    // Removes the element on top of the stack.    void pop()     {        if(q.empty())            return;        int len = q.size();        for(int i = 1 ; i < len ; i++)        {            int tmp = q.front();            q.pop();            q.push(tmp);        }        q.pop();    }    // Get the top element.    int top()     {        int len = q.size();        for(int i = 1 ; i < len ; i++)        {            int tmp = q.front();            q.pop();            q.push(tmp);        }        int tmp = q.front();        q.pop();        q.push(tmp);        return tmp;    }    // Return whether the stack is empty.    bool empty()     {        return q.empty();    }};



0 0
原创粉丝点击