栈和队列的问题

1. 有两个栈的队列:用两个栈来实现一个队列,从而使每个队列操作(enqueue(), dequeue()...)使用一个平均情况下的O(C)操作内完成。

    English version: Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.

解题思想:

        若你将元素全部入栈后再全部出栈,那么他们将以相反的顺序出现。若重复该操作,则他们又以原来的顺序出现了。

        If you push elements onto a stack and then pop them all, they appear in reverse order. If you repeat this process, they're now back in order.


2. Stack with max.  Create a data structure that efficiently supports the stack operations( push and pop) and also a return-the-maximum operation. Assume the elements are real numbers so that you can compare them.

Hint: Use two stacks, one to store all of the items and a second stack to store the maximums.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值