2018-10-22 Implement Queue by Two Stacks [M]

  1. Implement Queue by Two Stacks
    LC: 232
    Similar problem: Implement Stack by Two Queues [E]

As the title described, you should only use two stacks to implement a queue's actions.

The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

Example
push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2
Challenge
implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,167评论 0 10
  • 2018-08-06 国学诵读感恩日志1篇 2018年8月6日 星期一天气晴 今天开始系统读经第3周,第2天 读经...
    琳艳格格阅读 1,549评论 0 0
  • 名就 流言 礼赠
    挽笔阅读 1,850评论 0 0
  • 本来也没准备干啥,一大早被别人愚了,点开了一个伪装红包,顺势把微信转发公司娱乐群,顿时办公室响起一片笑声和笑骂声。...
    林荫儿阅读 3,003评论 0 0

友情链接更多精彩内容