现在的位置: 首页 > 综合 > 正文

栈和队列的问题

2013年10月26日 ⁄ 综合 ⁄ 共 626字 ⁄ 字号 评论关闭

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.

抱歉!评论已关闭.