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 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和2

(1)入栈:如果队列2当前有元素,而队列1为空(反之亦然),那么将需要入栈的元素放入队列1中,然后将队列2中的元素依次出队并入队到队列1中。(即要保证有一个队列是空的)

(2)出栈:将有元素(不为空)的队列出队即可-------如:

先将元素a插入队列1中 ,现在要将元素b入栈,则将b插入到队列2中然后将队列1中的a出队到队列2中,则队列2中的元素变为 b,a

这样队列1为空,现在要压入c, 则将c插入队列1中 ,依次将队列2中的b ,a出队并加入到队列1中 ,则队列1中的元素变为 c,b,a,而队列2为空

(保证一队列为空)

代码如下:

【两个队列】

class Stack {
public:
    // Push element x onto stack.
    queue<int> queue1;
    queue<int> queue2;
    void push(int x) {
        if (queue1.empty())
        {
            queue1.push(x);
            while(!queue2.empty()){
                int tmp = queue2.front();
                queue2.pop();
                queue1.push(tmp);
            }
        }else{
            queue2.push(x);
            while(!queue1.empty()){
                int tmp = queue1.front();
                queue1.pop();
                queue2.push(tmp);
            }
        }
    }  

    // Removes the element on top of the stack.
    void pop() {
        if (!queue1.empty())
            queue1.pop();
        if (!queue2.empty())
            queue2.pop();
    }  

    // Get the top element.
    int top() {
        if (!queue1.empty())
            return queue1.front();
        if (!queue2.empty())
            return queue2.front();
    }  

    // Return whether the stack is empty.
    bool empty() {
        return queue1.empty() && queue2.empty();
    }
};

 其他解法:

【两个队列】用两个队列myStack,temp实现一个栈。push时把新元素添加到myStack的队尾。pop时把myStack中除最后一个元素外逐个添加到myStack中,然后pop掉myStack中的最后一个元素,然后注意记得myStack和temp,以保证我们添加元素时始终向temp中添加。

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        myStack.push(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        std::queue<int> temp;
        int len = myStack.size();
        for(int i = 0; i < len - 1; i++) {
            temp.push(myStack.front());
            myStack.pop();
        }
        myStack = temp;
    }

    // Get the top element.
    int top() {
        if(myStack.size() != 0) return myStack.back();
    }

    // Return whether the stack is empty.
    bool empty() {
        if(myStack.size() == 0) return true;
        else return false;
    }
private:
    std::queue<int> myStack;
};

  或:

【两个队列】

class Stack {
    queue<int> rev_q;
public:
    // Push element x onto stack.
    void push(int x) {
        queue<int> temp_q;
        temp_q.push(x);
        while (!rev_q.empty()) {
            temp_q.push(rev_q.front());
            rev_q.pop();
        }

        rev_q = temp_q;
    }

    // Removes the element on top of the stack.
    void pop() {
        rev_q.pop();
    }

    // Get the top element.
    int top() {
        return rev_q.front();
    }

    // Return whether the stack is empty.
    bool empty() {
        return rev_q.empty();
    }
};

【一个队列】---push时直接添加到队尾就好。pop和top时,把队列除最后一个元素外,逐个循环添加到队列的尾部。

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        unsigned int size = s.size();
        this->s.push(x);
        while (size--){
            s.push(s.front());
            s.pop();
            }
    }

    // Removes the element on top of the stack.
    void pop() {
        s.pop();
    }

    // Get the top element.
    int top() {
       return s.front();
    }

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

  

 附注:队列queue的成员函数

  • empty()判断队列空,当队列空时,返回true。
  • size()访问队列中的元素个数。
  • push()会将一个元素置入queue中。
  • front()会返回queue内的第一个元素(也就是第一个被置入的元素)。
  • back()会返回queue中最后一个元素(也就是最后被插入的元素)。
  • pop()会从queue中移除一个元素。[1] 
  • 注意:pop()虽然会移除下一个元素,但是并不返回它,front()和back()返回下一个元素但并不移除该元素。

 

 

时间: 2024-10-09 18:11:43

leetcode:Implement Stack using Queues的相关文章

[LeetCode][JavaScript]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 empt

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

[LeetCode] 225. Implement Stack using Queues Java

题目: 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 on

Java for LeetCode 225 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 s

(easy)LeetCode 225.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 s

【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 o

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

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 s

[leetcode] 225. 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 s

Leetcode 225 Implement Stack using Queues STL

用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的队列中取出front() 1 class Stack { 2 public: 3 queue<int> q[2]; 4 5 // Push element x onto stack. 6 void move(int x){ 7 while(!q[1-x].empty()){ 8 q[x].push