LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译

用栈来实现队列的下列操作。

push(x) —— 将元素x写入到队列的尾部
pop() —— 从队列首部移除元素
peek() —— 返回队列首部元素
empty() —— 返回队列是否为空

注意:

你必须使用一个只有标准操作的栈。

也就是说,只有push/pop/size/empty等操作是有效的。

栈可能不被原生支持,这取决于你所用的语言。

只要你只是用stack的标准操作,你可以用list或者deque(double-ended queue)来模拟栈。

你可以假设所有的操作都是有效的(例如,pop或peek操作不会被用在空队列上)。

原文

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).

分析

大家可以看到,我们一共需要写4个函数,其实有两个可以直接写出来。

大家看着下文的代码,首先定义两个stack,可以回顾一下题目的第一句话:

Implement the following operations of a queue using stacks.

其实中文博大精深,英文同样也是的,这里很好的透露了是用多个stack来描写一个queue。一个主攻、一个辅助,多好……

push的话,不论是栈还是队列,都是从后面推上去,就一行;empty也是一样的。

下面来看看如何取出第一个元素,对于队列,我们peek的应该是最先进去的,但对于队列,我们最先取出的确是最后进去的。

喔,我忘了,可能有童鞋不太了解栈和队列的各种操作以及区别,可以看看我的这篇文章,非常详细的解释了它们。

【算法】7 分不清栈和队列?一张图给你完整体会

所以再看下面的代码,如果栈中只有一个元素,那么毫无疑问,就是它了。

如果有多个元素,首先将它们全部转移到另一个栈中,这时候它的顺序会反过来,然后留下一个元素。

这个部分就是pop和peek的关键了,peek的话只是取出这个数据出来,这个元素还是应该要保存起来的,所以我们也把它给了s2;而pop中则是直接给弹出了,这个数据也就不会进入s2。

当上面的操作完成之后,再将s2的数据全部转移到s中,这时候刚刚被颠倒的数据又恢复了原先的顺序。

在测试代码的过程中,还额外写了Queue的size,其实也只是一行而已,那就是求s的size。

这道题如果已经写完看完了,可以继续下面这道题喔:LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

Ok,这篇文章因为是我晚上在床上靠着枕头写的,所以写的比较乱……之前四百多篇都是正儿八经坐着写的。

代码

class Queue {
public:
    stack<int> s, s2;
    // Push element x to the back of queue.
    void push(int x) {
        s.push(x);
    }

    // Removes the element from in front of queue.
    void pop(void) {
        if (s.size() == 1)
            s.pop();
        else {
            while (s.size() > 1) {
                s2.push(s.top());
                s.pop();
            }
            s.pop();
            while (s2.size() > 0) {
                s.push(s2.top());
                s2.pop();
            }
        }
    }

    // Get the front element.
    int peek(void) {
        if (s.size() == 1) return s.top();
        while (s.size() > 1) {
            s2.push(s.top());
            s.pop();
        }
        int temp = s.top();
        s2.push(s.top());
        s.pop();
        while (s2.size() > 0) {
            s.push(s2.top());
            s2.pop();
        }
        return temp;
    }

    // Return whether the queue is empty.
    bool empty(void) {
        return s.empty();
    }
};
时间: 2024-10-06 16:15:35

LeetCode 232 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 和 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

[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] 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 Java

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

(easy)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

Java [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. Note

Java for LeetCode 232 Implement Queue using Stacks

Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // Removes the element from in front of queue. public void pop() { stack.remove(0); } // Get the front element. public int peek() { return stack.get(0); } //

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

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 whet