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();
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值