[LeetCode] Implement Queue using Stacks 用栈来实现队列

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

    • You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
    • Depending on your language, stack may not be supported natively. You
      may simulate a stack by using a list or deque (double-ended queue), as
      long as you use only standard operations of a stack.
    • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

这道题让我们用栈来实现队列,之前我们做过一道相反的题目Implement Stack using Queues 用队列来实现栈,是用队列来实现栈。这道题颠倒了个顺序,起始并没有太大的区别,栈和队列的核心不同点就是栈是先进后出,而队列是先进先出,那么我们要用栈的先进后出的特性来模拟出队列的先进先出。那么怎么做呢,其实很简单,只要我们在插入元素的时候每次都都从前面插入即可,比如如果一个队列是1,2,3,4,那么我们在栈中保存为4,3,2,1,那么返回栈顶元素1,也就是队列的首元素,则问题迎刃而解。所以此题的难度是push函数,我们需要一个辅助栈tmp,把s的元素也逆着顺序存入tmp中,此时加入新元素x,再把tmp中的元素存回来,这样就是我们要的顺序了,其他三个操作也就直接调用栈的操作即可,参见代码如下:

class Queue {
public:
    // Push element x to the back of queue.
    void push(int x) {
        stack<int> tmp;
        while (!s.empty()) {
            tmp.push(s.top());
            s.pop();
        }
        s.push(x);
        while (!tmp.empty()) {
            s.push(tmp.top());
            tmp.pop();
        }
    }

    // Removes the element from in front of queue.
    void pop(void) {
        s.pop();
    }

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

    // Return whether the queue is empty.
    bool empty(void) {
        return s.empty();
    }

private:
    stack<int> s;
};

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-10-29 03:43:21

[LeetCode] Implement Queue using Stacks 用栈来实现队列的相关文章

[LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

leetcode Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

LeetCode Implement Queue using Stacks (数据结构)

题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack1中的所有元素转移到stack2中,栈顶自然就是队头了,再弹出. (3)返回队头.与(2)一样. (4)是否为空.判断两个栈是否同时为空即可. 只要保证stack2为空时才可以将stack1转移到stack2中,就可以保证两边并不会产生混乱而出错. 1 class Queue { 2 /* 3 //

LeetCode OJ:Implement Queue using Stacks(栈实现队列)

比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如果内容在stack2中,这时候将其倒回在进行push.这里采取第一种比较笨的方法,代码如下所示: 1 class Queue { 2 public: 3 // Push element x to the back of queue. 4 void push(int x) { 5 s1.push(x)

[LeetCode][JavaScript]Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

【LeetCode】232. Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p